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

To find a value from a file

Status
Not open for further replies.

prog3

Programmer
Jun 3, 2003
9
US
Hi,

I am new to unix scripting and need help for the following question. Any kind of help is appreciated.

The file I have is of the following format.

GROUP_FIELD_NAME:DocumentNumber
GROUP_FIELD_VALUE:1
GROUP_FIELD_NAME:policyNumber
GROUP_FIELD_VALUE:295
GROUP_FIELD_NAME:AccountNumber
GROUP_FIELD_VALUE:500
GROUP_FIELD_NAME:DocumentNumber
GROUP_FIELD_VALUE:2
GROUP_FIELD_NAME:policyNumber
GROUP_FIELD_VALUE:4456
GROUP_FIELD_NAME:AccountNumber
GROUP_FIELD_VALUE:5001
.
.
.
GROUP_FIELD_NAME:DocumentNumber
GROUP_FIELD_VALUE:109
GROUP_FIELD_NAME:policyNumber
GROUP_FIELD_VALUE:29
GROUP_FIELD_NAME:AccountNumber
GROUP_FIELD_VALUE:5013

I need the last Field_value of the Field_Name DocumentNumber. In this case it is 109. This is the only value I need from the Input file.
Any solution how I can code it?

Thanks in advance.
 
Code:
awk -F: '$1=="GROUP_FIELD_VALUE" { print $2 }' file.txt
 
prog3:

You can do it with awk although I don't think Salem's solution is exactly what you want. I decided to let the shell do it:

#!/bin/ksh

# parse fixed field data
readval=0
while IFS=: read f1 fn
do
if [[ $fn = "DocumentNumber" ]]
then # search for document number
readval=1
continue
fi
if [[ $readval -eq 1 ]]
then # and save the next value
lastval=$fn
readval=0
fi
done < file.txt
echo $lastval # print only the last one
# end script

Regards,

Ed
 
Try this:
Code:
dn=`awk -F: '$2==&quot;DocumentNumber&quot;{getline;dn=$2}END{print $2}' YourFileName`
echo Last DocumentNumber=$dn


Hope This Help
PH.
 
Hi Guys,

Thanks for all your responses. It has been most helpful.
I tried all the 3 solutions.

Salem's code - I get all the values. Since, I wanted only the Last value of the Document number, I did not how to change the awk command.

olded - The code worked perfectly.

PHV - I tried this code. But, the variable dn does not return anything. I have coded as follows,
#!/bin/sh

RPT_TO=/home/x96206/bal

cd ${RPT_TO}

echo &quot;To print the document number from the pass file&quot;

dn=`awk -F: '$2==&quot;DocumentNumber&quot;{getline;dn=$2}END{print $2}' cnt.pass`
echo Last DocumentNumber= $dn

When I execute, I get the following
To print the document number from the pass file
Last DocumentNumber=

Any suggestions?

Thanks again for all your help.

 
> PHV - I tried this code. But, the variable dn does not return anything. I have coded as follows,
I think there's a typo in the END { } section

Code:
dn=`awk -F: '$2==&quot;DocumentNumber&quot;{getline;dn=$2}END{print dn}' YourFileName`
echo Last DocumentNumber=$dn
 
Thanks a lot, Salem. It worked correctly.

prog3
 
Thanks Salem for the typo

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top