awk print to file

I have a big c file that gives a lot of output that I don't care about right now. I wrote this awk script to do mostly what I want. Not sure how to put the finishing touches on it. I would also like it to write to a file when it gets the appropriate condition as true. Is this possible with awk? Or do I need to use a different program?

    ./a.out 50 5 4 | 
    awk '{
    if ($1 =="success")
        {
        print "success";
        print $3;
        //write to success file
        }
    if ($1 =="failure")
        {
        print "failure";
        print $3;
        //write to failure file
        }
    if ($4 =="page")
        print "page";
        //write to page file
    }'
    ./a.out 50 5 4 | 
    awk '{
    if ($1 =="success")
        {
        print "success";
        print $3;
        print "Writing success" > "success.file"
        }
    if ($1 =="failure")
        {
        print "failure";
        print $3;
        print "Writing failure" > "failure.file"
        }
    if ($4 =="page")
        print "page";
        print "Writing to page" > "page.file"
    }'

Edit: Added forgotten "" around file names so it doesn't think it is an invalid variable name.

1 Like

Yes, in awk you can direct your output to a file:

awk '
  .....
  some_condition {
    print "success\n" $3 > "some_file"
  }
  .....
'
1 Like

Even if success , failure , page , and file are variables that contain strings consisting of decimal digits, <period> is not an operator in awk that can be used to join two strings. The names of files used in print output redirections have to be strings or variables that expand to non-empty strings.

I would guess that the request was for something more like:

    ./a.out 50 5 4 | 
    awk '{
    if ($1 =="success")
        {
        print "success" > "success.txt"
        print $3 > "success.txt"
        }
    if ($1 =="failure")
        {
        print "failure" > "failure.txt"
        print $3 > "failure.txt"
        }
    if ($4 =="page")
        print "page" > "page.txt"
    }'

If you have a script that wants to write to several files (typically, more than nine), you may have to worry about closing and reopening files and considering whether you want to append to existing files or overwrite them if you need to reopen an output file; but for just three output files, you don't have to worry about that.

1 Like

Hi Don,

Thank you for caching the forgotten double quotes around the file name.

Not clear if the success/failure files should be written on top of stdout or in place. Try

awk '
BEGIN           {S = "#"
                 SRCH = S "success" S "failure" S
                }
SRCH ~ S $1 S   {printf "%s\n%s\n", $1, $3 > $1 ".txt"
                }
$4 == "page"    {printf "%s\n", $4 > $4 ".txt"
                }
' file

Some awk's cannot handle this and will return a syntax error. Either use parentheses or use variables that contain the file name:

printf "%s\n%s\n", $1, $3 > ($1 ".txt")

or

f=$1 ".txt"; printf "%s\n%s\n", $1, $3 > f