I have multiple files with different number or records in each. I want an awk command to print the second last line of them and perform some change. I want something like this:(this one doesn't work of course)
awk '( NR == FNR-1 ), $0","' *.txt
I have multiple files with different number or records in each. I want an awk command to print the second last line of them and perform some change. I want something like this:(this one doesn't work of course)
awk '( NR == FNR-1 ), $0","' *.txt
awk reads records as they come and has no notion of how far from the end those records are if it has not read them yet (extra records could very well be added after it reads and processes the current record).
Contrary to sed, it cannot even tell which is the last record (sed has the $ address which it actually implements by internally reading one record in advance so it knows which is the last one).
You can however do some processing at the end in the special END statement, or with GNU awk, after having processed each input file (in the ENDFILE statement).
So you can save the last two records while you're processing them, and then in the END/ENDFILE statement, recall the penultimate one from where you've saved it.
For instance:
awk '{prevlast = last; last = $0}
END {if (NR >= 2) print "penultimate:", prevlast}' < input
Or:
gawk '{prevlast = last; last = $0}
ENDFILE {
if (FNR >= 2) print "penultimate for", FILENAME ":", prevlast
}' file1 file2
Or to generalise it for the nth from the end:
awk -v n=2 '{saved[NR % n] = $0}
END {if (NR >= n) print saved[(NR + 1) % n]}' < input
gawk -v n=2 '{saved[FNR % n] = $0}
ENDFILE {if (FNR >= n) print saved[(FNR + 1) % n]}' file1 file2
To append comma , at the end of second-to-last line with modifying each file in-place.
sed solution:
Sample file1.txt:
C2-C1 1.5183
C3-C2 1.49
C3-C1 1.4991
O4-C3 1.4104
C1-C2-C3 59.78
Sample file2.txt:
C2-C1 1.5052
C3-C2 1.505
C3-C1 1.5037
S4-C3 1.7976
C1-C2-C3 59.95
sed -i 'x; ${s/.*/&,/;p;x}; 1d' file*.txt
Viewing results:
$ head file[12].txt
==> file1.txt <==
C2-C1 1.5183
C3-C2 1.49
C3-C1 1.4991
O4-C3 1.4104,
C1-C2-C3 59.78
==> file2.txt <==
C2-C1 1.5052
C3-C2 1.505
C3-C1 1.5037
S4-C3 1.7976,
C1-C2-C3 59.95