I would like to process a list of files matching: GPS*\.xyz with an awk script. I would then like to output the files to GPS*\.xyz.out (e.g. the same file name appended with .out). Something like:
awk '{if(NR==1) {offset=-$1}; $1=$1+offset; print }' GPS*.xyz
this does exactly what I want EXCEPT redirecting the output appropriately. I tried
awk '{ ... print > FILENAME.out}' GPS*.xyz
and
awk '{...}' GPS*.xyz > GPS*.xyz.out
but as expected this did not work.
I realize that putting this in:
for f in ls GPS*.xyz
loop would work as I need but I would like an elegant solution that I can learn from.
Suggestions?
Cheers,
Brandon Franzke
franzke:
I would like to process a list of files matching: GPS*\.xyz with an awk script. I would then like to output the files to GPS*\.xyz.out (e.g. the same file name appended with .out). Something like:
awk '{if(NR==1) {offset=-$1}; $1=$1+offset; print }' GPS*.xyz
Since you are reading several files, NR probably should be FNR:
awk 'FNR==1 {offset=-$1;next} {$1+=offset; print >FILENAME".out"}' PS*.xyz
r2007
July 14, 2005, 8:32pm
3
awk 'FNR==1 {offset=-$1;if(NR>1) close(of);of=FILENAME".out"} {$1+=offset; print >of}' GPS*.xyz
futurelet:
Since you are reading several files, NR probably should be FNR:
awk 'FNR==1 {offset=-$1;next} {$1+=offset; print >FILENAME".out"}' PS*.xyz
close() may be needed to prevent having too many files open.
awk 'FNR==1 {offset=$1;close(f);f=FILENAME".out";next}{$1-=offset; print >f}' GPS*.xyz
The next is there becasue I assume you don't want the line with the offset to be printed.