I am trying to edit multiple files using awk in the following way:
awk '{F = FILENAME ".inp"; print $0 > F ; if(NR==3) print "%moinp usr/speciale/br/brhgooh/scan/newscan/freqBZ/cas/prov2/",FILENAME > F }' *.xyz
It works well with the first file, but in the rest of the files the change does not appear.
Any suggestions?
>Solution :
You need to use FNR not NR
NRis the record number of all records seen thus farFNRis the record number of the current file
Adding some whitespace helps readability:
awk '
BEGINFILE {
close(F)
F = FILENAME ".inp"
}
{print > F}
FNR == 3 {print "%moinp usr/speciale/br/brhgooh/scan/newscan/freqBZ/cas/prov2/" FILENAME > F }
' *.xyz
If your awk does not have BEGINFILE, you can use FNR == 1 instead.
Other changes:
printinstead ofprint $0($0 is the default)print "%mo .../" FILENAMEwithout a comma — a comma will insert a space after the slash.close(F)to prevent "too many open files" errors