Is awk a better tool for this task? 
Author Message
 Is awk a better tool for this task?

I have a file in the following sort of format:

RECORDSTART
NO1=4444444
No2=34532342
No3=64335353
RECORDEND
RECORDSTART
NO1=2342524
NO2=0809809890
No3=67876234
RECORDEND

Which I turn into the following output file:

NO1             NO3
=========================
4444444         64335353
2342534         67876234

I use a variety of unix tools including cut, split etc to create the
output file.  I've been told by a friend that the way I achieve the results
is
very long winded.  He says that if I use awk I could achieve
the same results with just a few lines of code.  I've looked at
O'Reilly's book but not really being an awk expert I can't see
how this can be easily done.  Anybody know how?  My current
method splits each record into seperate files, numbers the lines in
the files, uses grep and a bunch of other stuff to eventually produce
the required output.

Cheers,

Ian Corban



Wed, 28 Mar 2001 03:00:00 GMT  
 Is awk a better tool for this task?

Quote:

>I have a file in the following sort of format:

>RECORDSTART
>NO1=4444444
>No2=34532342
>No3=64335353
>RECORDEND
>RECORDSTART
>NO1=2342524
>NO2=0809809890
>No3=67876234
>RECORDEND

>Which I turn into the following output file:

>NO1             NO3
>=========================
>4444444         64335353
>2342534         67876234

>I use a variety of unix tools including cut, split etc to create the
>output file.  I've been told by a friend that the way I achieve the results
>is
>very long winded.  He says that if I use awk I could achieve
>the same results with just a few lines of code.  I've looked at
>O'Reilly's book but not really being an awk expert I can't see
>how this can be easily done.  Anybody know how?  My current
>method splits each record into seperate files, numbers the lines in
>the files, uses grep and a bunch of other stuff to eventually produce
>the required output.

Here's one way:

awk -F= 'BEGIN{print "NO1             NO3\n========================="}
/^N[oO]1/ {one=$2}
/^N[oO]3/ {three=$2}
/^RECORDEND/ {print one "         " three}' infile > outfile

There are others, using printf (which would give you better
output format control) instead of print.

This uses infile for your data and redirects the result to outfile.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Wed, 28 Mar 2001 03:00:00 GMT  
 Is awk a better tool for this task?
Place the following script in a file, say script.awk:

BEGIN {print "NO1             NO3\n=========================";FS="="}
/NO1/ {NO1 = $2}
/NO3/ {printf "%-16s%-16s\n", NO1, $2}

Then at the command line, run:

awk -f script.awk inputdatafile > outputdatafile

It's that simple.

The first line prints out your header information, and also sets the
field separator to the character "=".

The next line searches for a line in the data file with the pattern
NO1, and when it finds it, stores the second field (everything after
the =) into a variable named NO1.

The last line searches for the pattern NO3, and then prints out the
stored data from the NO1 line plus the data from the NO3 line.  The
stuff immediately following the printf is the formatting string for
the printout.

I have made a couple of assumptions about your data:

1.  There is exactly one NO3 line for each NO1 line.
2.  The beginning of the lines is actually NO1 and NO3, not No1 and
No3 (note capitalization).

If either of these assumptions is incorrect, the script can be
adjusted as needed.

Another good source of awk information can be found at:
http://w4.lns.cornell.edu/public/compdoc/info/gawk/gawk_toc.html

Quote:

>>I have a file in the following sort of format:

>>RECORDSTART
>>NO1=4444444
>>No2=34532342
>>No3=64335353
>>RECORDEND
>>RECORDSTART
>>NO1=2342524
>>NO2=0809809890
>>No3=67876234
>>RECORDEND

>>Which I turn into the following output file:

>>NO1             NO3
>>=========================
>>4444444         64335353
>>2342534         67876234

>>I use a variety of unix tools including cut, split etc to create the
>>output file.  I've been told by a friend that the way I achieve the results
>>is
>>very long winded.  He says that if I use awk I could achieve
>>the same results with just a few lines of code.  I've looked at
>>O'Reilly's book but not really being an awk expert I can't see
>>how this can be easily done.  Anybody know how?  My current
>>method splits each record into seperate files, numbers the lines in
>>the files, uses grep and a bunch of other stuff to eventually produce
>>the required output.

>>Cheers,

>>Ian Corban


--
Greg

http://www.mastnet.net/~jupiter


Thu, 29 Mar 2001 03:00:00 GMT  
 Is awk a better tool for this task?
: I have a file in the following sort of format:

: RECORDSTART
: NO1=4444444
: No2=34532342
: No3=64335353
: RECORDEND
: RECORDSTART
: NO1=2342524
: NO2=0809809890
: No3=67876234
: RECORDEND

: Which I turn into the following output file:

: NO1             NO3
: =========================
: 4444444         64335353
: 2342534         67876234

: I use a variety of unix tools including cut, split etc to create the
: output file.  I've been told by a friend that the way I achieve the results
: is
: very long winded.  He says that if I use awk I could achieve
: the same results with just a few lines of code.  I've looked at
: O'Reilly's book but not really being an awk expert I can't see
: how this can be easily done.  Anybody know how?  My current
: method splits each record into seperate files, numbers the lines in
: the files, uses grep and a bunch of other stuff to eventually produce
: the required output.

: Cheers,

: Ian Corban

Three lines:
awk 'BEGIN { printf "%-16s%s\n%s\n","NO1","NO2","=========================" }
/1=/ { sub(/^.*=/,""); printf "%-16s", $0}
/3=/ { sub(/^.*=/,""); print}'

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.



Thu, 29 Mar 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. awk.py, an AWK-like tool for Python users

2. AWK vs Perl For Misc Data Processing Tasks

3. Awk task

4. Help needed in complex awk task

5. Debugging tools for tasking

6. Task/Calendar tool

7. Better task safe collections

8. Would like to know what languages are good for what tasks

9. I am just learning verilog any good books or websites

10. Lynx is good for Ada tasking ?

11. Is python a good choice for this task?

12. best way to do some simple tasks

 

 
Powered by phpBB® Forum Software