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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parsing input into separate files 2

Status
Not open for further replies.

arclox

MIS
Feb 9, 2001
3
US
:) Hi!

I am searching for a simpler way to parse input into separte files.

The input, where the fields are separated by a pipe, looks like this:
1|Barron's|\n* Location: PERIODICAL|\n* Location: v.74 (1994)-v.80 (2000) Micro
fiche\n* Location: RESERVES\n* Location: Latest issues located in Periodical Res
erves.
2|0047-8318|Ms|\n* Location: PERIODICAL|\n* Location: v.1 (1972)-v.8 (1980) Bou
nd\n* Location: v.9 (1980)-v.18 (1989) Microfiche
2|0047-8318|Ms|\n* Location: PERIODICAL|\n* Location: v.1 (1990/1991)-v.9 (1999/
1999) Bound\n* Location: v.1 (1990/1991)-v.4 (1994) Microfiche\n* Location: La
test issues located in Current Periodicals.

I'm writing a Bourne shell script that includes the following awk instructions in order to place groups of records into separate files for specific processing. I am using groupid from the first field to parse them into files. Since this file is small, I can do this, but if there were many groups and many records this would become cumbersome.

The awk instructions are:
awk -F"|" '{if ($1 == 1) print $0}' fixfieldawk.out > barrons
awk -F"|" '{if ($1 == 2) print $0}' fixfieldawk.out > ms
awk -F"|" '{if ($1 == 3) print $0}' fixfieldawk.out > lancet
awk -F"|" '{if ($1 == 4) print $0}' fixfieldawk.out > currentbio
awk -F"|" '{if ($1 == 5) print $0}' fixfieldawk.out > japysiology
awk -F"|" '{if ($1 == 6) print $0}' fixfieldawk.out > jquarterly
awk -F"|" '{if ($1 == 7) print $0}' fixfieldawk.out > libraryj

This is so repetitive and I was wondering if anyone might know of a better way.

Thanks
 
Awk is simple and beautiful. Script in pure awk is simpler:

BEGIN { FS = "|" }

$1 == 1 { print $0 > "barrons" }
$1 == 2 { print $0 > "ms" }
# ... other


God is Love.

Bye




 
# arclox-
#
#
# file: simple
#
# Usage: simple fixfieldawk.out <extension> <CR>
#
# Notes:
# We have to append to the output files.
#
# Otherwise, only the last line is output to each file.


ext=$2 # pass in unique file extension each time
# you run from command line so previous
# file doesn't get clobbered!

#!usr/bin/sh


awk 'BEGIN{FS=&quot;|&quot;}

{
grouplist = &quot;barrons,ms,lancet,currentbio,japysiology,jquarterly,libraryj&quot;

split (grouplist,groups,&quot;,&quot;)

for ( i = 1; i <= 7; i++ ) {
if ( $1 == i ) print >> groups&quot;.'&quot;$ext&quot;'&quot;
}

}' $1


# Hope this helps you!
#
# Jesse

flogrr
flogr@yahoo.com

 
Jesse,

Thanks! This is exactly what I was looking for. I
knew it could be done with a loop. However, since I am fairly new to
awk, I wasn't sure how to put them into files with awk other than with
a basic statement. I really appreciate your help.

Lisa.
 
floggr,

Thanks! This is exactly what I was looking for. I knew it could be done with a loop. However, since I am fairly new to awk, I wasn't sure how to put them into files with awk other than with a basic statement. I really appreciate your help.

arclox
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top