Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output redirection 2

Status
Not open for further replies.

goldenradium2001

Technical User
Mar 22, 2002
91
US
Can anyone help me on this syntax? Here's my file called file1:

one two three
four five six
seven eight nine

First, I execute this command:
nawk '{print $1, $2 > "output"}' file1

The contents of the output file is:

one two
four five
seven eight

Next I execute this command:
nawk '{print $3 >> "output"}' file1

The contents now of the output file are:
one two
four five
seven eight
three
six
nine

How do I make it so that the column $3 is actually appended to the right of the first and second columns?

Thanks!
 
Hi goldenradium2001,

Try this solution:

awk '

FILENAME == "output" { lines[++i] = $0 }

FILENAME == "file1" { fld3[++j] = $3 }

END { for( k = 1; k <= i; k++ )
printf (&quot;%s %s\n&quot;, lines[k], fld3[k]) }' output file1 > file2

Hope this helps


flogrr
flogr@yahoo.com

 
Do you have to do it using only awk ?

This should work:
Code:
nawk '{print $1, $2 > &quot;output1&quot;}' file1
nawk '{print $3 > &quot;output2&quot;}' file1
paste -d&quot; &quot; output1 output2 > output
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top