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!

Parsing multiple records 1

Status
Not open for further replies.

geirendre

Vendor
Aug 13, 2001
603
NO
Hi, I'm new to AWK and trying to parse through a long list of records and extract fields from them.

sample record:
DN 0
TYPE CDP
STCD TSC

DN 2203
CPND
CPND_LANG ROMAN
NAME
XPLN 27
DISPLAY_FMT FIRST,LAST
TYPE 500
TN 008 0 09 02 MARP DES SAS 17 FEB 2003

DN 2204
CPND
CPND_LANG ROMAN
NAME
XPLN 27
DISPLAY_FMT FIRST,LAST
TN 008 0 09 03 MARP DES SAS 17 FEB 2003

DN 7906
CPND
CPND_LANG ROMAN
NAME
XPLN 27
DISPLAY_FMT FIRST,LAST
TYPE MIX
TN 002 0 10 06 DES SAS 1 DEC 2000
TN 016 0 10 03 KEY 00 MARP DES ROM 7 FEB 2000
(2008)


I'm looking to make a liste like:

DN TN
0
2203 008 0 09 02
2204 008 0 09 03
7906 002 0 10 06
7906 016 0 10 03


Have tried this:
Code:
awk ' BEGIN { RS="" }
{ print "DN\tTN"  ( i=1 ) }
{
while (i == 1) {
/^DN\ +/ ( dn=$2 )
/^TN\ +/ ( loop=$2 && shelf=$3 && card=$4 && unit=$5 )
printf("%d\t%d %d %d %d\n",dn,loop,shelf,card,unit);
i++
}
}' $1

Clearly, I'm in deep water here...
;-)
Can someone please help?
 
A starting point:
Code:
awk '
BEGIN{t=-1;print "DN\tDT"}
/^DN /{if(++t)print d;d=$2}
/^TN /{t=-1;print d"\t"$2" "$3" "$4" "$5}
' $1


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Excelent PHV, that's a GOOD[/color blue] starting point :)
Can you please explain the use of the t=-1, (++t), t=-1 part ?
If I would like to change the print part so to not use $2 $3 etc, but rader state someting like { print "everyting after what's matched in the /PATTERN/ part" } what would I use?
The pattern (not only TN) can span multiple fields you see, and I need to print the rest of the line.

 
Can you please explain the use of the t=-1, (++t), t=-1 part ?
The issue was to print the DN value even with no TN value, so I've played with a tracking flag (t).
t=-1 means we're in an initial state (no DN nor TN)
t=0 means we have a DN value and are waiting for TN (or next DN)
t>0 means we've got a new DN and haven't print the previous DN

print "everyting after what's matched in the /PATTERN/ part"
typed, untested:
/^TN /{t=-1;x=$0;sub(/^TN +/,d"\t",x);print x}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top