Shell – Awk – output the second line of a number of .dat files to one file

awkcommand lineio-redirectionshell-scripttext processing

I have multiple files something like: (in reality i have 80)

file1.dat

2 5

6 9

7 1

file2.dat

3 7

8 4

1 3

I want to end up with a file containing all of the second lines. i.e.

output.dat

6 9

8 4

What i have so far loops though the file names but then over-writes the file before it. e.g. the output of the above files would just be

8 4

my shell script looks like this:

post.sh

TEND = 80

TINDX = 0

while [ $TINDX - lt $TEND]; do

awk '{ print NR==2 "input-$TINDX.dat > output.dat

TINDX = $((TINDX+1))

done

Best Answer

Remove while loop and make use of shell brace expansion and also FNR, a built-in awk variable:

awk 'FNR==2{print $0 > "output.dat"}' file{1..80}.dat
Related Question