Re: Parse an input file using a Key value
- From: Ed Morton <morton@xxxxxxxxxxxxxx>
- Date: Mon, 20 Mar 2006 18:53:31 -0600
justin.ponting@xxxxxxxxx wrote:
Ed Morton wrote:
justin.ponting@xxxxxxxxx wrote:
Hi,
I am trying to achieve the following task using a shell script:
I have an input file in the following format getting repeated.
0 ASM "FNB002179-test.asm;0;0:"
ATTR Type="STRING" Key="ItemDesc" Value=""
ATTR Type="STRING" Key="ItemID" Value="FNB002179"
ATTR Type="STRING" Key="ItemModDate" Value="15-Mar-2006 19:23"
ATTR Type="STRING" Key="Level" Value="0"
ATTR Type="STRING" Key="RevID" Value="1"
ATTR Type="STRING" Key="RevModDate" Value="15-Mar-2006 19:23"
ATTR Type="STRING" Key="DB_PART_NO" Value="FNB002179"
ATTR Type="STRING" Key="DB_PART_REV" Value="1"
ATTR Type="STRING" Key="__PLM_ITEMREV_UID" Value="hsCRBaU3RpGBhB"
ATTR Type="STRING" Key="DB_OCC_UID" Value="AAAAAAAAAAAAAA"
And I need the output in the following format for all the entries as
below:
Level, ItemID,RevID,RevModDate, ItemModeDate
0, FNB002179, 1, 15-Mar-2006 19:23, 15-Mar-2006 19:23
1, FNB002181, 1, 15-Mar-2006 19:21, 15-Mar-2006 19:21
1, FNB002180, 1, 15-Mar-2006 19:23, 15-Mar-2006 19:23
2, FNB002182, 1, 15-Mar-2006 19:21, 15-Mar-2006 19:21
2, FNB002181, 1, 15-Mar-2006 19:21, 15-Mar-2006 19:21
1, FNB002181, 1, 15-Mar-2006 19:21, 15-Mar-2006 19:21
I tried to achieve this task using the following script below but it is
failing. Can someone please point out the mistake in the script here?
Or would some one recommend a way to do this?
a) You didn't tell us in what way the script is failing so "someone"
would need to figure that out, and
b) You didn't tell us what the transoformation is you need to apply to
the input to get the desired output, so "someone" would need to figure
that out too.
So, your choices are:
1) just hope you to catch the eye of "someone" with a lot of free time
to figure out the above, or
2) provide the missing details.
Regards,
Ed.
Ed,
Sorry for the wrong use of my language.
The script reads the input file, check for the required "Key variables"
and print their corresponding Values.
Regards, Justin.
Here's a soluiton to a similair problem that came up some time ago that you should be able to massage to your needs:
Transposing rows to selected columns and sorting by key.
Given the following input file:
Number of executions = 437
Number of compilations = 1
Worst preparation time (ms) = 1
Best preparation time (ms) = 1
Rows deleted = 0
Number of executions = 1
Number of compilations = 1
Worst preparation time (ms) = 4
Best preparation time (ms) = 4
Rows deleted = 0
Number of executions = 29
Number of compilations = 1
Worst preparation time (ms) = 1
Best preparation time (ms) = 1
Rows deleted = 0
To tranpose certain rows into columns and sort by one of the
column, like the following which is sorted by "Number of executions":
Number of executions Number of compilations Rows deleted
437 1 0
29 1 0
29 1 0
This will do it all in gawk:
gawk -vRS="" -F"\n" 'BEGIN{ fields = "1 2 5"; key = "1"
numflds = split(fields,flds," ")
}
{
for (i=1; i<=NF;i++) {
split($i,f,"=")
# Get rid of all spaces from the end of the title text
sub(/[[:blank:]]*$/,"",f[1])
title[i]=f[1]
# Get rid of all spaces from the value field
value[i]=f[2]+0
# Determine the width for this column based on the width
# of the title text plus 3 for spacing. Left-justify (%-).
fmt[i]="%-"(length(title[i])+3)"s"
}
# We will want to sort on the key column so we need to create a
# string at the start of each line to sort on later. Take the key
# columns value and pad it with zeros up to 20 chars followed by
# a space to separate it fromthe first real column. Conversion of
# "7" to "0007" and "17" to "0017" is necessary because asort()
# is alphabetical not numerical so all numeric fields must be the
# same width to compare alphabetically.
lines[NR] = sprintf("%020s ",value[key])
# Now add the real columns, formatted as determined earlier.
for (i=1; i<=numflds; i++) {
lines[NR] = lines[NR] sprintf(fmt[flds[i]], value[flds[i]])
}
}
END {
# Print the title line
for (i=1; i<=numflds; i++) {
printf fmt[flds[i]], title[flds[i]]
}
print ""
# Sort the lines alphabetically, i.e. by the value of the key column
# added above to the front of each line.
asort(lines)
# Print each line
for (i=1; i<=NR; i++) {
# strip out the first numeric value, the key value added above
sub("[[:digit:]]* ","",lines[i])
print lines[i]
}
}'
Setting fields and key at the beginning obvious dictates which fields to
be printed and which key to sort on. The only thing it assumes about field sizes is that the key fields values won't be more than 20 characters.
.
- References:
- Parse an input file using a Key value
- From: justin . ponting
- Re: Parse an input file using a Key value
- From: Ed Morton
- Re: Parse an input file using a Key value
- From: justin . ponting
- Parse an input file using a Key value
- Prev by Date: Sum of du -sk
- Next by Date: Re: strange behaviour of "mv" command
- Previous by thread: Re: Parse an input file using a Key value
- Next by thread: strange behaviour of "mv" command
- Index(es):
Relevant Pages
|
|