Archive for the ‘sed-awk-perl’ Category

Print every lines of a file where last field value is > N

July 23, 2011

Consider the following scenario where the last filed of each line is basically the time taken to server a request by the app server – in our case it is ( say time=400ms). Now I want to see those lines which takes more than 400ms to server the request – here is the trick

cat prod_debug.log | grep ‘PROFILER’ | awk ‘{ sub(/time=/,””);print}’ | awk ‘{sub(/ms/,””);print}’ | awk ‘$NF>400’

Advertisements

Merging two corresponding line with sed on a file

July 23, 2011

Merging two lines

sed ‘$!N;s/\n/ /’ file1 > file2

Deleting blank lines –

 sed ‘/^$/d’  file1 > file2

Deleting all lines starting with ‘–‘

 sed ‘/^\–/d’  file1 > file2

 

Quick way to sum every numbers in a file written line by line

October 1, 2010

If you have a file full of numbers written line by line, you can sum every line to get the total.

With a file like this:

34

9

6

91

23

you will get:

163

And the shell command to get this done is –

( sed ‘s/^/b+=/’  your-file ; echo b ) | bc

Or,

cat your file | awk ‘{ sum += $0 } END {print sum}’

Conditional statement with awk

August 11, 2010

Sometimes bash failed to compare long integer. where awk language can easily compare integer of any range here is the way –

awk ‘BEGIN { if ( ‘$warn’ > ‘$crit’ )
{printf “Unknown.crit must be larger than warn\n”
exit 1}
if ( ‘$used’ < ‘$warn’ )
{printf “OK.Free Space: ‘$free_t’ GB, Space Left ‘$percent’%\n”
exit 2}
if ( ‘$used’ > ‘$warn’ && ‘$used’ < ‘$crit’ )
{printf “Warning. Free Space: ‘$free_t’ GB,Space Left ‘$percent’%\n”
exit 3}
if ( ‘$used’ > ‘$crit’ )
{printf “Critical. Free Space: ‘$free_t’ GB,Space Left ‘$percent’%\n”
exit 4}
}’

more on `sed`

June 7, 2009

1. Deleting 2-5 line from a file

sed ‘2,5d’ file.txt

2. Deleting from line 25 to end of a file

sed ‘2, \$d’ file.txt

3. Printing 5-8 line of a file

sed -ne 5,8p file.txt

4. Printing 6 to end

sed -ne “6, \$p” file.txt

5. Printing line 4, 7, 22

sed -ne 4p -e 7p -e 22p file.txt

6. Delete lines from a file matching a pattern –

sed -e ‘/pattern/d’ oldfile \> newfile