how to read one file, print to two files 
Author Message
 how to read one file, print to two files

Hi,

How can I use awk to read one file and print the output into two files? For
example, if a line contained the text "gotofileone", print the line to the
first file, otherwise print the output to the second file. I think I have
seen this in the faq, but do not fully understand how it works in awk. I am
using a ksh script with embedded awk code in aix.

Michael



Mon, 17 Feb 2003 22:00:42 GMT  
 how to read one file, print to two files

Quote:

> Hi,

> How can I use awk to read one file and print the output into two files? For
> example, if a line contained the text "gotofileone", print the line to the
> first file, otherwise print the output to the second file. I think I have
> seen this in the faq, but do not fully understand how it works in awk. I am
> using a ksh script with embedded awk code in aix.

> Michael

Micheal,

You can redirect the outputs like this:

 /gotofileone/{ print >> "file1.txt"}
!/gotofileone/{ print >> "file2.txt"}

Tristan.



Mon, 17 Feb 2003 22:58:09 GMT  
 how to read one file, print to two files


Quote:
> How can I use awk to read one file and print the output into two
files? For
> example, if a line contained the text "gotofileone", print the line to
the
> first file, otherwise print the output to the second file. I think I
have
> seen this in the faq, but do not fully understand how it works in awk.

awk '
/gotofileone/{ print > "File1"; continue; }
{ print > "File2"; }
' InputFile

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 17 Feb 2003 23:10:06 GMT  
 how to read one file, print to two files

Quote:

> How can I use awk to read one file and print the output into two files? For
> example, if a line contained the text "gotofileone", print the line to the
> first file, otherwise print the output to the second file.

# untested
{ file = /gotofileone/ ? "file1.txt" : "file2.txt"; print >file }

Don't unnecessarily repeat the regular expression pattern match
operation. And don't use a 'continue' statement outside of a
'while', 'for', or 'do' loop: it won't work.

Always prefer one print statment to two print statements in simple
filters. What changes from one line to the next is the output file,
not the fact that you're printing the line.

--
Jim Monty

Tempe, Arizona USA



Tue, 18 Feb 2003 05:14:15 GMT  
 how to read one file, print to two files
Hi All,

Excellent, thank you for that.

Regards,

Michael



Tue, 18 Feb 2003 21:39:21 GMT  
 how to read one file, print to two files
Hi Jim,

Quote:
> # untested
> { file = /gotofileone/ ? "file1.txt" : "file2.txt"; print >file }

I have tested the above in Linux, using gawk, and it works fine thanks.

Michael



Wed, 19 Feb 2003 09:17:25 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. how to print two file in one time ??

2. read two sets of data from two spread sheet files to two arrays problem

3. merging 2 columns from two files in one file

4. one file unit accessing two different files?

5. print multiple files from one source file

6. Writing two equal files without creating two files in dct

7. print to screen and file with one print statement

8. two files one script

9. Reports with One Parent and Two Child files(multiple records eacy)

10. ABC Browse - Two related files in one window

11. Two data files in one browse?

12. Two browse boxes with same file on one window

 

 
Powered by phpBB® Forum Software