Reputation: 10366
I have a loop that processes a bunch of files within a dir. I would like to input the filename into the file it processes, but I'm getting an error. It works perfectly with the myvar
syntax but I need that for obvious reasons.
awk: cmd. line:1: RS=
awk: cmd. line:1: ^ unexpected newline or end of string
for filename in $files
do
awk -v "myvar=${filename}"
RS= '/-- Ticket/{++i; print "PROMPT myvar Line ",
i ORS $0 ORS; i+=split($0, a, /\n/)+1}' ${filename}.txt
done
Upvotes: 0
Views: 76
Reputation: 10039
If you could avoid a batch loop, it's better (performance mainly for subshell fork, ...)
# assume TXT_files is the list of $files with .txt extension (not purpose of this OP)
awk RS='' '
/-- Ticket/{
# get the file name without extension
myvar = FILENAME;sub( /\.txt$/,"",myvar)
print "PROMPT " myvar " Line " ++i ORS $0 ORS
i += split( $0, a, /\n/) + 1
}
' ${TXT_files}
Upvotes: 0
Reputation: 85530
Couple of issues here, use the -v
syntax for each of the variables that you are trying to pass to awk
,
awk -v myvar="${filename}" -v RS= '/-- Ticket/{++i; print "PROMPT " myvar " Line ", i ORS $0 ORS; i+=split($0, a, /\n/)+1}' ${filename}.txt
# ^^^ variable1 ^^^^^ variable2 --> using separate -v for each
should be right approach.
For a shell variable import to awk
do it as in my example above, not as "myvar=${filename}"
but just myvar="${filename}"
Upvotes: 1