Linux Shell Tricks (print odd/even lines, linux cut, bc etc)

I had to parse through complex logs and need to calculate differences of hex values. The file contained SEND and RECV keywords in each line and hex values after that. I was interested only in “RECV” values. For example,

05:23:04    SEND    4    8021 0300    [C/R] GetNextBuildLabelChar
05:23:04    RECV    4    8021 A8D1    0x8021: [C/R] GetNextBuildLabelChar, 0xA8D1: (-22319s, 43217u)
05:23:04    SEND    4    8021 0400    [C/R] GetNextBuildLabelChar
05:23:04    RECV    4    8021 3D43    0x8021: [C/R] GetNextBuildLabelChar, 0x3D43: (15683)
05:23:04    SEND    4    8021 0301    [C/R] GetNextBuildLabelChar
05:23:04    RECV    4    8021 14C6    0x8021: [C/R] GetNextBuildLabelChar, 0x14C6: (5318)
05:23:04    SEND    4    8021 0401    [C/R] GetNextBuildLabelChar
05:23:04    RECV    4    8021 3D49    0x8021: [C/R] GetNextBuildLabelChar, 0x3D49: (15689)

To do that, here is a shell command

cat logs.txt | grep RECV | cut -d' ' -f2 | awk -F" " '{print $1}' > logs1.txt

This will print all the hex values. But, the 1st line contains LSB, 2nd MSB, 3rd LSB, 4th MSB etc. So here is the command to print odd and even lines of the file.


#To print even lines
awk "NR%2==0" logs1.txt > logs_MSB.txt
#To print odd lines
awk "NR%2!=0" logs1.txt > logs_LSB.txt

Now, need to join all the lines.

pr -tmJ logs_MSB.txt logs_LSB.txt > logs_hex.txt

The logs_hex.txt will contain white spaces between LSB and MSB. To remove it, do following:

tr -d ' \t' < logs_hex.txt > logs_nospace.txt

Finally, need to convert all hex into decimal..

echo "ibase=16;`cat logs_hex.txt`" | bc

Sigh…

Advertisements

2 thoughts on “Linux Shell Tricks (print odd/even lines, linux cut, bc etc)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s