Extract a particular column value from multiple files
ls -ltr
-rwxr-xr-x 4 dc staff 131 Feb 27 21:15 test.txt
-rwxr-xr-x 4 dc staff 134 Feb 25 21:15 test1.txt
test.txt and test1.txt (similar structure) contains a table structure like
cat test.txt
RECORD #1 DETAILS
sl no. regno name age
1 20 ABC 10
cat test1.txt
RECORD #2 DETAILS
sl no. regno name age
1 21 DEF 11
I want to extract the 2nd column value from all .txt files and store it into some other files.
Ouput.txt should be
test.txt 20
test1.txt 21
It’s not exactly clear what you are looking for, but if you just want to print the second column of the 4th line (and that is the ambiguity, as it’s not clear if you always want the data from line 4, or the data from 3 lines after ^RECORD, or data from the line after each occurrence of "sl no.", etc), you could do:
$ awk 'FNR == 4 { print FILENAME, $2 }' test.txt test1.txtor, if you are using an awk that does not support FILENAME (at the moment, I’m not sure if that is standard or a gnu extension) and you are not using csh or one of its cousins, you could do:
$ for n in test.txt test1.txt; do printf '$s ' $n; awk 'NR==4{ print $2}' $n; done