awk print to file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk print to file
# 1  
Old 12-14-2015
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?


Code:
    ./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
    }'

# 2  
Old 12-14-2015
Code:
    ./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.

Last edited by Aia; 12-14-2015 at 02:01 AM..
This User Gave Thanks to Aia For This Post:
# 3  
Old 12-14-2015
Yes, in awk you can direct your output to a file:
Code:
awk '
  .....
  some_condition {
    print "success\n" $3 > "some_file"
  }
  .....
'

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 12-14-2015
Quote:
Originally Posted by Aia
Code:
    ./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
    }'

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:
Code:
    ./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.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 12-14-2015
Hi Don,

Thank you for caching the forgotten double quotes around the file name.
# 6  
Old 12-14-2015
Not clear if the success/failure files should be written on top of stdout or in place. Try
Code:
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

# 7  
Old 12-14-2015
Quote:
Originally Posted by RudiC
Not clear if the success/failure files should be written on top of stdout or in place. Try
Code:
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:
Code:
printf "%s\n%s\n", $1, $3 > ($1 ".txt")

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

2. Shell Programming and Scripting

awk print even fields of file

Hello: I want to print out the even number of fields plus the first column as row identifiers. input.txt ID X1 ID X2 ID X3 ID X4 A 700 A 1200 A 400 A 1300 B 2000 B 1000 B 2000 B 600 C 1400 C 200 C 1000 C 1200 D 1300 D 500 D 600 D 200and the output is: output.txt ID X1 X2 X3... (3 Replies)
Discussion started by: yifangt
3 Replies

3. Shell Programming and Scripting

awk script to print file name

I have the following awk script that looks at the first 2 columns of multiple files and when they match, it prints the min of the 3rd column. for year in tave-{1950..2015}.txt do cat "$year" done | awk '{n=$3; $3=x} !($0 in A) || n<A {A=n} END{for(i in A) print i A}' > output.txt ... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

4. Shell Programming and Scripting

Not getting array in .awk file and print it

I have test.sh file as below : set -A IDARR $ID echo | awk -f test.awk -v TempArr="${IDARR }" I have test.awk file as below : BEGIN { Flag = 1; } { print "Hello"; for(i in TempArr) { print i; } } (9 Replies)
Discussion started by: nes
9 Replies

5. Shell Programming and Scripting

Awk: Print count for column in a file using awk

Hi, I have the following input in a file & need output as mentioned below(need counter of every occurance of field which is to be increased by 1). Input: 919143110065 919143110065 919143110052 918648846132 919143110012 918648873782 919143110152 919143110152 919143110152... (2 Replies)
Discussion started by: siramitsharma
2 Replies

6. UNIX for Advanced & Expert Users

Awk to print values of second file

Hello, I have a data file with 300,000 records in it, and another file which contains only the line numbers of roughly 13,000 records in the data file which have data integrity issues. I'm trying to find a way to print the original data by line number identified in the second file. How can I do... (2 Replies)
Discussion started by: peteroc
2 Replies

7. Shell Programming and Scripting

AWK filter from file and print

Dear all, I am using awk to filter some data like this:- awk 'NR==FNR{a;next}($1 in a)' FS=":" filter.dat data.dat >! out.dat where the filter and input data look like this:- filter.dat... n_o00j_1900_40_007195350_0:n_o00j_1940_40_007308526... (3 Replies)
Discussion started by: atb299
3 Replies

8. Shell Programming and Scripting

awk print lines in a file

Dear All, a.txt A 1 Z A 1 ZZ B 2 Y B 2 AA how can i use awk one line to achieve the result: A Z|ZZ B Y|AA Thanks (5 Replies)
Discussion started by: jimmy_y
5 Replies

9. Shell Programming and Scripting

using awk to print some columns of a file

Hi, i have a file with content 00:01:20.613 'integer32' 459254 00:01:34.158 459556 00:01:36.626 'integer32' 459255 and i want to print only output as below 00:01:20.613 459254 00:01:34.158 459556 00:01:36.626 459255 i dont want the word 'integer32' which is the second column. i... (2 Replies)
Discussion started by: dealerso
2 Replies

10. Shell Programming and Scripting

print variable in file using awk

hi, i have store variable in $var i want to print it in $3 of file using awk. how can i do it? file : var1,var2,var3 var5,var6,var7 var8,var9,var10 . . . i want to print $var in $3 ( for example var3 or var7 or var10) thanks (3 Replies)
Discussion started by: kamel.seg
3 Replies
Login or Register to Ask a Question