PDA

View Full Version : awk once again



abraxas334
September 22nd, 2009, 03:56 PM
I really should try understanding manuals better ....
anyway my problem is this:
I want to print from 1000 files like this file.*.dat the last line into a single file i.e. the new files ends up with 1000 last lines of the 1000 file.*.dat.

I got as far as printing it from one file



awk 'END{print}' file.1.dat > newFile


however



awk 'END{print}' file.*.dat > newFile

also produces just one line in the new file! Any ideas please?

Thank you

falconindy
September 22nd, 2009, 04:24 PM
You'll need to create a loop. Provided all 1000 files are in the same directory, and it's your pwd:

for f in `ls *.dat`; do
awk 'END{print}' $f >> /path/to/newFile
done

Hmm, I might also be wrong about awk not accepting wildcards... Use '>>' instead of '>'.

geirha
September 22nd, 2009, 04:25 PM
Not sure how to do that with awk, but with GNU tail, you can do:

tail -q -n 1 file.*.dat > newFile

@falconindy. Don't loop over the output of ls, it breaks for files with spaces and special characters, and is also completely unnecessary. And second, remember to quote parameter expansions for the same reason (preserve spaces and special characters in filenames)


for f in *.dat; do awk 'END{print}' "$f"; done > newFile

abraxas334
September 22nd, 2009, 04:32 PM
Not sure how to do that with awk, but with GNU tail, you can do:

tail -q -n 1 file.*.dat > newFile

[/COLOR]$f"; done > newFile[/code]

Thanks that was great :) it worked really well!
Another one to write down and try to remember!

falconindy
September 22nd, 2009, 04:34 PM
Not sure how to do that with awk, but with GNU tail, you can do:

tail -q -n 1 file.*.dat > newFile

@falconindy. Don't loop over the output of ls, it breaks for files with spaces and special characters, and is also completely unnecessary. And second, remember to quote parameter expansions for the same reason (preserve spaces and special characters in filenames)


for f in *.dat; do awk 'END{print}' "$f"; done > newFile
Mmm, good point. Tail is a much better solution. Thanks.