Udit Gupta Udit Gupta - 7 months ago 14
Bash Question

Extract last and second last strings of a file in shell variables

Although it is looking similar to my previous post but here purpose is different.

udit@udit-Dabba ~/ah $ cat decrypt.txt
60 00 00 00 00 17 3a 20 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 02 *00 00 e0 f9 6a 61 61 6e
65 6b 61 68 61 6e 67 61 79 65 77 6f 64 69 6e* 00
00 00 03 29


I want to extract last string of the file (here it is
29
) in a shell varaible

I tried this ...

size=`wc -w encrypt.txt`

awk -v size=$size 'BEGIN {RS=" ";ORS=" ";}' {if (NR>size-1 &&
NR < size+1)print $0}' decrypt.txt


Output :
29

But when I changed the file slightly ..

udit@udit-Dabba ~/ah $ cat decrypt.txt
60 00 00 00 00 17 3a 20 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 02 *00 00 e0 f9 6a 61 61 6e
65 6b 61 68 61 6e 67 61 79 65 77 6f 64 69 6e* 00
65 6b 61 68 61 6e 67 61 00 00 03 29


Output :
03

Why there is discrepency between the results ??

I am new to awk and shell features so I am not sure whether it is a right way to do so or not ???

I think there should be some variation of
grep,sed,awk
or any other linux command which may solve my problem but I am not aware of it.

Please guide me for this.

Thanx in advance.

Purpose :

Make two variables in a shell script which should store last and second last strings of an input file.

Limitation :

Every input file contains a blank line at the end of file.

(Like in above mentioned file , after the file contents there would be one more blank line just like hitting
ENTER
key and that can not be changed because it is being generated through a C program at run time.)

Answer

To get the last field:

awk '{ if (NF > 0) { last = $NF } } END { print last }' "$@"

The second last field is trickier for the case where there is just one field on the last line (so you need the last field from the line before).

awk '{ if (NF > 0)
       {
           if (NF == 1) { lastbut1 = last; last = $1; }
           else { lastbut1 = $(NF-1); last = $NF; }
       }
     }
     END { print lastbut1 " " last; }' "$@"

This produces a blank and the last value if the file contains but one value. It produces just a blank if there are no values at all.