Get N'th line from multiple files in Linux
The task I am trying to accomplish, I have files like this:
test1.csv test2.csv test3.csv etc.
And I want to get the 3'rd line of each file. Right now, I can get 3'rd lines using awk, or sed like
echo | awk 'FNR == 3 { print; exit }' test1.csv >> last_file.csv
or with sed or tail.
But when I try to do this on multiple files, it cannot get the lines. I want to do this,
echo | awk 'FNR == 3 { print; exit }' test*.csv >> last_file.csv
How can I achieve this?
Thank.
source to share
You must use
awk 'FNR == 3 { print; nextfile }' test*.csv >> last_file.csv
The problem is, when used, exit
it completely disables awk from processing input. nextfile
tells awk to stop processing the current file and move on to the next file. The command echo
, since you are using it, is not required.
More details here:
http://www.gnu.org/software/gawk/manual/html_node/Nextfile-Statement.html
source to share