Operating System - HP-UX
1833875 Members
3131 Online
110063 Solutions
New Discussion

parsing text by scripting

 
SOLVED
Go to solution
Sammy_2
Super Advisor

parsing text by scripting

I want to parse a text file below (either in perl or shell scripting) so that I get
the last record that has the id no. The
first field within double quotes is always id no.
The input text file is below and the output I want to see is in the end. Can't figure it out.?
Help please.

INPUT FILE:
==========================
{"123", "sam", "Test"}
{"123", "sami", "Test Only"}
{"123", "samuel", "This is the record I want"}
{"345", "sharo19", "VICC eassocr"}
{"678", "pat", "Test2"}
{"678", "patrick1", "Real Test now"}
{"321", "sharo19", "VICC eassocr"}
===============================

OUTPUT NEEDED FROM THE ABOVE INPUT FILE is:
===========================================
{"123", "samuel", "This is the record I want"}
{"345", "sharo19", "VICC eassocr"}
{"678", "patrick1", "Real Test now"}
{"321", "sharo19", "VICC eassocr"}
===========================================
good judgement comes from experience and experience comes from bad judgement.
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: parsing text by scripting

Hi Sam:

Try this:

#!/usr/bin/sh
typeset MYFILE=$1
awk -F, 'BEGIN {TOG=0;D=$1;S=$0}
{if (TOG==1 && D!=$1)
{print S} TOG=1;D=$1;S=$0}
END {print S}' $MYFILE
exit 0
#.end.

Pass your input file as an arguement to the script above and you will get the desired output.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor
Solution

Re: parsing text by scripting

Hi (again) Sam:

Oops. The 'awk' BEGIN rule (although harmless) is superfluous. Actually no I/O has occured at that point and I should have dropped it, simplfying to:

#!/usr/bin/sh
typeset MYFILE=$1
awk -F, '{if (TOG==1 && D!=$1) {print S} TOG=1;D=$1;S=$0}
END {print S}' $MYFILE
exit 0
#.end.

Regards!

...JRF...

linuxfan
Honored Contributor

Re: parsing text by scripting

Hi Sam,

Here's another way to do it,


/Begin/
#!/usr/bin/sh

if [[ $# < 1 ]]
then
echo "Usage: $0 filename"
exit 1
fi

cat $1 |sort -n |cut -d \" -f 2 |uniq |while read id
do
grep "$id" "$inputfile" |tail -1
done

/End/

To run it
script.sh filename

-HTH
Ramesh
They think they know but don't. At least I know I don't know - Socrates
linuxfan
Honored Contributor

Re: parsing text by scripting

Oops, small typo
This should definitely work

/Begin/
#!/usr/bin/sh

if [[ $# < 1 ]]
then
echo "Usage: $0 filename"
exit 1
fi

cat $1 |sort -n |cut -d \" -f 2 |uniq |while read id
do
grep "$id" "$1" |tail -1
done

/End/

-Regards
Ramesh
They think they know but don't. At least I know I don't know - Socrates
Curtis Larson_1
Valued Contributor

Re: parsing text by scripting

cat yourFile |
awk '{
split($0,array,"\"");
id=array[2];
bigA[id]=$0;
} END {
for ( i in bigA )
print bigA[i];
}'
Sammy_2
Super Advisor

Re: parsing text by scripting

Thanks to James, Ramesh and Curtis.
All of your script did what I asked for .
good judgement comes from experience and experience comes from bad judgement.