awk – print last field

 

NF is built in variable in awk which stores Number of fields.

To get last field with awk use following example:

$ echo a b c | awk '{print $NF}'

Output:
c

By default awk will take any white space character(space, tab, …) to split.

To get last but one field with awk:

$ echo a b c | awk '{print $(NF-1)}'

Output:
b

To print only number of fields with awk:

$ echo a b c | awk '{print NF}'

Output:
3

mutt – send mails with attachments

 

By using mutt we can send mails with attachments from command line.

Install mutt:

$ apt-get install mutt

Send mail with attachment:

$ mutt -s "PFA" user@example.com -a attachment.txt < body.txt

or

$ echo "body text here" | mutt -s "PFA" user@example.com -a attachment.txt

Send mail without attachment:

$ mutt -s "PFA" user@example.com < body.txt

or

$ echo "body text here"  | mutt -s "PFA" user@example.com

awk/sed print from line number to end of file

I have file with 10 lines where content of first line in 1, second line 2, third line is 4 …… and tenth line is 10.

Here I will show some operations related to read read in lines based their line numbers.

Print from line number 5 to end of file:

$ awk 'NR>=5' inputFile
or
$ sed '1,4d' inputFile

Print from line number 4 to 9 or print lines between linen number 3 and 10:

$ awk 'NR>=4 && NR<=9'  inputFile
or
$ sed -n '4,9p' inputFile

Print all even lines numbers:

$ awk '{if(NR%2 == 0) {print $1}}' inputFile

Print all odd lines numbers:

$ awk '{if(NR%2 != 0) {print $1}}' inputFile

Bash divide floating numbers

 

Dividing floating point number with bash/shell is bit tricky task.

To divide floating point number  we can use bc command.

Lets try with example, here I will divide 1.1 with 2 that is 1.1/2

By simply using bc you will get output as 0, try following command you should get output as 0.

$ echo "1.1/2" | bc

Output:

0

To get output as floating point number we need to use -l option with bc as shown below:

$ echo "1.1/2" | bc -l

Output:

.55000000000000000000

To truncate length of output we can use scale option with above expression.

Lets try to truncate length to 2 with following example:

$ echo "scale=2;1.1/2" | bc -l

Output:

.55

Lets try to truncate length to 2 with following example:

$ echo "scale=5;1.1/2" | bc -l

Output:

.55000

 

Grep multiple patterns

 

Grep will support pattern matching.

Example:

To grep b to f from string abcdefghijklmnopqrstuvwxyz using following example:

$ echo "abcdefghijklmnopqrstuvwxyz" | grep -P -o "b.*?f"

Output:

bcdef

With grep we can also match multiple patterns with a single expression. Lets try to print two patterns b to f and p to v by using following example:

$ echo "abcdefghijklmnopqrstuvwxyz" | grep -P -o "(b.*?f)|(p.*?v)"

Output:

bcdef
pqrstuv

gnome terminal open new tab with script from GUI

To open multiple tabs in new window use following script:

gnome-terminal \
--tab -e "tail -f file" \
--tab -e "ssh user@hostname" \
--tab -e "vim file"

This command will open new terminal window with 3 tabs and in first tab command “tail -f file” will executed, in second tab “ssh user@hostname” will executed and in third tab “vim file” will executed.

Add above command in a script lets call it as tmp.sh

Now enable execute permission for script tmp.sh to execute from GUI using tutorial I posted on one of my previous post Run shell/bash script from GUI.

After enabling execute permissions from GUI just double click on the scrip and select run option, this should open new terminal window with tabs. In each tab a specified command will executed.

Run shell/bash script from gui

 

By default when we double click on shell/bash script it will open with gedit.

To execute a shell/bash script from GUI use following procedure:

  • Right click on the script file and select properties.
  • Go to Permissions tab.
  • Now enable Execute by clicking Allow executing files as program.
  • After updating execute permissions, again double click your script, it will show following popup

exec

  • Select Run in Terminal/Run option in above popup, this will start executing script.

Count number of lines in a file – Linux

 

There are different ways to count number of lines in a file.

I will show the some of commands to count number files in file.

With wc:

$ wc -l <inutFileName>

Counting number of lines with wc -l is most common and popular way.

With awk:

$ awk 'END{ print NR }' <inutFileName>

With sed:

$ sed -n '$=' <inutFileName>

With grep:

$ grep -c "" <inutFileName>

-Sany

Linux shell/bash check if string is empty or not

To check if a string is empty/null or not use -n string or -z string with if condition.

-n string will return true if string is not empty/null.

-z string will return true if string is empty/null.

Example:

str="abc"

if [[ -n $str ]] ; then echo "not empyt/null"; fi

Since str is not empty, above command will return not empyt/null.

str=""

if [[ -z $str ]] ; then echo "empyt/null"; fi

Since str empty, above command will return empyt/null.

-Sany