Μηνιαία αρχεία: Απρίλιος 2016


Bash Function to print out the files and the lines that contain a needle

The following code will create a function in bash that accepts two parameters (1: the place to search in, 2: the value to search for).

You can place it in your ~/.bashrc file to have it available whenever you open a bash shell.

#1. Copy/paste the below lines in your .bashrc

#takes 2 parameters (1: the haystack to search in, 2: the needle)
# Will print out the files and the lines that contain the needle
xfind(){
  FIND_VAR="$2";
  STACK="$1";
  if [ -f "$STACK" ] || [ -d "$STACK" ]; then
    find "$STACK" \
      -exec grep --color "$FIND_VAR" -sl '{}' \; \
      -exec grep "$FIND_VAR" -s '{}' \;
  else
    echo "ERROR: No file or folder with the name '$STACK' exist";
  fi
}
#2. Run source ~/.bashrc -- to reload 

Usage examples:

xfind . "bar";
xfind /etc/ "conf";

Recursively change extension of multiple files using find

Assuming you have a whole bunch of files that you need to change their extension from one to another, you can use the following commands after setting the values for the BEFORE and the AFTER variables to the values you need.

BEFORE='.txt'; AFTER='.csv'; find . -type f -name "*$BEFORE" -exec bash -c 'mv "$1" "${1%$2}$3"' _ '{}' "$BEFORE" "$AFTER" \;

What the above will do is: after setting the two input variables it will call find in the current directory (with recursion) and find all files that their suffix is the value you set in BEFORE. Then, for each match it will create a new shell terminal in which it will rename the file by removing the old suffix and then attaching the new one. We pass the input variables as parameters to the new shell and that is why inside the code of the shell we are using variables $1, $2 and $3. The reason we had to issue a new shell is because we wanted to reuse the ‘{}’ variable.


NotePad++: Remove multi-line (/**/) comments automatically 4

We are going to use NotePad++ replace functionality (CTRL+H) to remove all multi-line comments that are enclosed in a pair of /* and */.

While in the source file you want to edit, open up the ‘Replace’ window either from the ‘Search’ menu or by using CTRL+H.

Set ‘Search Mode’ to ‘Regular expression’ and tick the ‘. matches newline’.

In the ‘Find what:’ input type /\*(.*?)\*/\n and leave the ‘Replace with:’ input empty.

NotePad++ Remove Multi-Line CommentsTo find the next comment, you can click the ‘Find Next’ button and then hit ‘Replace’.

To make the change on all comments just hit ‘Replace All’.


Fedora/Bash: Get the IP of enp0s3

Following is a small snippet that will print on screen the IP of enp0s3 (or any other device if you change the name) while in Fedora.
As you will see, it is not a very sound solution as it depends on the structure of the output of ifconfig enp0s3.

Nevertheless is works (for Fedora at least)! 🙂

ifconfig enp0s3 | grep "inet " | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | cut -d ' ' -f 2

What this line does is: first it prints out the configuration information for enp0s3, then finds the line that contains the inet, then using sed it will trim the result (in other words, it will remove all leading and all trailing white-space from the pipe), finally cut gets the second column of the data after separating the line using the space symbol.

The Fedora version that was used for this tutorial is

$cat /etc/fedora-release 
Fedora release 23 (Twenty Three)

The version of ifconfig for this tutorial is

$ifconfig --version
net-tools 2.10-alpha

In case you want to assign the IP of enp0s3 to a variable, you can easily do as follows

IP=`ifconfig enp0s3 | grep "inet " | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | cut -d ' ' -f 2`;