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

Search Line 1

Status
Not open for further replies.

smicro

MIS
Dec 26, 2002
281
US
Have a file that one line reads

848844 retyor toyoyih W=9099 eoeoe

One of the lines will always contain W=
I can grep for W= but it pulls out the entire line, how do I grep for this to just pull out W=9099
The columns and position of W= are changing constantly, tired using awk.

 
smicro,
something like
Code:
cp /dev/nul /yourfolder/yournewfilename
cat /yourfolder/yourfilename | while read column1 column2 column3 column4 column5
do
if column4 = "W=9099"
then
print column4 >> yournewfilename
fi
done
untested but should work
regards,
longhair
 
Faster, use if you have gnu grep, let grep both filter and slice and dice:

grep -o 'W=\w*' < inputfile.log

Example to test with:

Code:
echo -e "foo bar W=zot blah grunt\nmoo\n cow\n grunt W=783737 groan\nrabbit dog W= basselope" | grep -o 'W=\w*'

will output:

W=zot
W=783737

Cheers!
 
More portable, if you don't have gnu grep:

grep -E 'W=\w*' | sed -e 's/^.*\(W=[01-9a-zA-Z]*\).*/\1/'

Example to test with:

Code:
echo "foo bar W=zot blah grunt\nmoo\n cow\n grunt W=783737 groan\n" | grep -E 'W=\w*' | sed -e 's/^.*\(W=[01-9a-zA-Z]*\).*/\1/'


will output:

W=zot
W=783737

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top