Saturday, February 18, 2012

Run UNIX bash loop with nohup


Suppose we have to run following UNIX bash shell loop construct under 'nohup'
$ for i in $(seq 20); do echo $i;./somescript.sh $i; done

Running the above loop under nohup directly will not work as 'nohup' expects a single-word command and its arguments. This is how we can run a UNIX bash shell loop construct using 'nohup'.
$ nohup sh -c 'for i in $(seq 20); do echo $i;./somescript.sh $i; done' &

From man page of 'sh'
-c               Read commands from the command_string operand instead of from the
standard input. Special parameter 0 will be set from the command_name operand
and the positional parameters ($1, $2, etc.) set from the remaining argument operands.

Related post:
- Setsid : Run UNIX program in a new session
- More about UNIX seq command

Friday, October 28, 2011

Print line number with unix less command

Less command introduction:
As you know Unix less command writes the contents of a file onto the screen a page at a time and this is one of the utilities using which one can view the content of a file without opening it in an editor.
Press the [space-bar] if you want to see another page, and type [q] if you want to quit reading.

This is how we can print line number with Unix less command.

1)
From man page of CAT(1):
       -n, --number
number all output lines

So,
$ cat -n file.txt | less

will print the line number in-front of each line.

2)
From man page of LESS(1):
       -N or --LINE-NUMBERS
Causes a line number to be displayed at the beginning of each line in the display.

So,
$ less -N file.txt

will do the same as 1) above.

3) One can set the following to print line number with less by default:
$ export LESS='-RS#3NM~g'




Related posts:
- Numbering lines in a file using Bash awk
- Open file at required line number in Unix vi editor

Monday, June 27, 2011

Awk - convert epoch to date in the same file

Example 1)
Input file:
$ cat test1.txt
2|Z|1219071600|AF|0
3|N|1219158000|AF|89
4|N|1220799600|AS|12
1|Z|1220886000|AS|67
5|N|1220972400|EU|23
6|R|1221058800|OC|89

Required output:
2|Z|Mon 18 Aug 2008 03:00:00 PM UTC|AF|0
3|N|Tue 19 Aug 2008 03:00:00 PM UTC|AF|89
4|N|Sun 07 Sep 2008 03:00:00 PM UTC|AS|12
1|Z|Mon 08 Sep 2008 03:00:00 PM UTC|AS|67
5|N|Tue 09 Sep 2008 03:00:00 PM UTC|EU|23
6|R|Wed 10 Sep 2008 03:00:00 PM UTC|OC|89

i.e. convert the UNIX epoch values on 3 rd field of the above file to standard human readable date format using Awk.

The Awk solution using "strftime" function:
$ awk 'BEGIN {FS=OFS="|"}{$3=strftime("%c",$3)} {print}' test1.txt

More about strftime format specifiers can be found here

I have created a python program to achieve the above output and the script is here

Example 2)
Input file:
$ cat test2.txt
2|Z|time:1219071600|AF|0
3|N|time:1219158000|AF|89
4|N|time:1220799600|AS|12
1|Z|time:1220886000|AS|67
5|N|time:1220972400|EU|23
6|R|time:1221058800|OC|89

Required output:
2|Z|time:Mon 18 Aug 2008 03:00:00 PM UTC|AF|0
3|N|time:Tue 19 Aug 2008 03:00:00 PM UTC|AF|89
4|N|time:Sun 07 Sep 2008 03:00:00 PM UTC|AS|12
1|Z|time:Mon 08 Sep 2008 03:00:00 PM UTC|AS|67
5|N|time:Tue 09 Sep 2008 03:00:00 PM UTC|EU|23
6|R|time:Wed 10 Sep 2008 03:00:00 PM UTC|OC|89

The awk solution:
$ awk 'BEGIN {FS=OFS="|"}{$3="time:"strftime("%c",substr($3,6,10))} {print}' test2.txt

Related posts:
- Awk Substr function explained
- Print first character of a field in a file using Awk
- Replace first 5 characters of a file using UNIX Sed and Awk
- Convert fixed length file to a CSV file using Awk

© Jadu Saikia http://unstableme.blogspot.com