tags:

views:

54

answers:

3

I need help in moving the contents printed by awk to a text file. THis is a continuation of previous quesion I have 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?

+1  A: 

Sorry, not much of an awk buff, but shouldn't the shell handle that for you?

awk YOUR_AWK_STUFF_HERE >> appended.file.name
Daren Thomas
+2  A: 

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.

ghostdog74
Ya thanks very much
Harish
+1  A: 

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.

JXG