Operating System - HP-UX
1833882 Members
2220 Online
110063 Solutions
New Discussion

Re: Grepping pattern from a File

 
SOLVED
Go to solution
Virumandi
Frequent Advisor

Grepping pattern from a File

Hi ,

I want to grep a particular pattern from a file...

The string will be like this...

multimediaGroup type="media" dbid="525"

I want to grep the dbid value say 525 from the file ...

How can I do it with sed or perl..
9 REPLIES 9
Virumandi
Frequent Advisor

Re: Grepping pattern from a File

I dont know the dbid value But i am sure that it is a three digit numeric number..
Peter Godron
Honored Contributor
Solution

Re: Grepping pattern from a File

Hi,
if your input file format is consistent:
cat input.dat | grep dbid | awk -F'"' '{print $4}'

Please also start rewarding answers to your questions:
http://forums1.itrc.hp.com/service/forums/helptips.do?#28
Michal Toth
Regular Advisor

Re: Grepping pattern from a File

consistency is the key here ;-)
john korterman
Honored Contributor

Re: Grepping pattern from a File

Hi,

grep is grep:

grep "dbid=\"[0-9][0-9][0-9]\"$" /your_infile


regards,
John K.
it would be nice if you always got a second chance
Virumandi
Frequent Advisor

Re: Grepping pattern from a File

Thanks for the command.
Its working fine..

But One more query,
Can we save the value in a variable..

Virumandi
Frequent Advisor

Re: Grepping pattern from a File

Hi Peter..,

Your solution works but We cant able to assign the value we grepped to the variable..

Can u guide to me how to do the same.

Thanks...
Peter Godron
Honored Contributor

Re: Grepping pattern from a File

Hi,
if you only find a single dbid in the file:
a=`grep dbid | awk -F'"' '{print $4}'`

Otherwise:
while read record
do
a=`echo $record | grep dbid | awk -F'"' '{print $4}'`
echo "This is your variable value" $a
done < input.dat
Arturo Galbiati
Esteemed Contributor

Re: Grepping pattern from a File

Hi,
this retrun teh value in the variable dbid

dbid=$(awk -F"dbid=" '/dbid=/ {print $2}' file)

This command is indipendent by teh dbid position on the line. It founds line containg 'dbid=' and print the subsequent field. BY awk is not necessary to use cat and/or grep etc. awk is able to open a file.


HTH,
Art
Virumandi
Frequent Advisor

Re: Grepping pattern from a File

Thanks to all...