awk output to a file
I need help in moving the contents printed by awk to a text file. THis is a continuation of previous quesion I have开发者_JAVA技巧 to move all the contents into the same file so it is appending.
To be specific
nawk -v file="$FILE" 'BEGIN{RS=";"}
/select/{ gsub(/.*select/,"select");gsub(/\n+/,"");print file,$0;}
/update/{ gsub(/.*update/,"update");gsub(/\n+/,"");print file,$0;}
/insert/{ gsub(/.*insert/,"insert");gsub(/\n+/,"");print file,$0;}
' "$FILE"
How to get the print results to a text file appended one after the other in the same file?
I/O redirection are pretty basic when working with the shell. If you want to append lines, use >>
. eg
nawk -v file="$FILE" 'BEGIN{RS=";"}
/select/{ gsub(/.*select/,"select");gsub(/\n+/,"");print file,$0;}
/update/{ gsub(/.*update/,"update");gsub(/\n+/,"");print file,$0;}
/insert/{ gsub(/.*insert/,"insert");gsub(/\n+/,"");print file,$0;}
' "$FILE" >> newfile
please spend time reading up on shell scripting if you haven't yet.
Sorry, not much of an awk buff, but shouldn't the shell handle that for you?
awk YOUR_AWK_STUFF_HERE >> appended.file.name
Other answers suggest shell redirection. Actually, awk
allows redirection as part of the print
statement.
nawk -v file="$FILE" 'BEGIN{RS=";"}
/select/{ gsub(/.*select/,"select");gsub(/\n+/,"");print file,$0 >> results.txt;}
/update/{ gsub(/.*update/,"update");gsub(/\n+/,"");print file,$0 >> results.txt;}
/insert/{ gsub(/.*insert/,"insert");gsub(/\n+/,"");print file,$0 >> results.txt;}
' "$FILE"
From the awk
man page:
The print statement prints its arguments on the standard output (or on a file if >file or >>file is present or on a pipe if |cmd is present), separated by the current output field separator, and terminated by the output record separator. file and cmd may be literal names or parenthesized expressions; identical string values in different statements denote the same open file.
精彩评论