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

grep 1

Status
Not open for further replies.

icu812

MIS
Sep 10, 2001
52
US
I'd like to be able to search the first colon delimited field in a file that has the following lines in it(file name is nfs.lis):
sp30:sp16:/stage:/stage
sp30:sp13:/mksysb:/mksysb1
sp16:sp13:/mksysb:/mksysb

For example: I want to grep sp16 nfs.lis and I want it to return just the sp16:sp13:/mksysb:/mksysb line. I do not want the sp30:sp16:/stage:/stage line to come up int the search. How can I accomplish this??????
 
There is proably a better way to do this,
but here's one way...

cat $FILE|while read LINE
do
1ST_FIELD=`echo $LINE|awk -F: '{print $1}'`
RESULT=`echo $1ST_FIELD|grep "sp16 nfs.lis"`
if [ ${#RESULT} -gt 0 ]
then
echo $LINE
fi
done
Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
Any of these will work. The first expects the line to start with sp16. The next three work the same if the regexp ("sp16") is embedded in the first colon-delimited field. I'd go with the awk example.

grep ^sp16 nfs.lis

awk -F: '"sp16" ~ $1 {print}' nfs.lis
sed '/^[^:]*sp16[^:]*/\!d' nfs.lis
grep "^[^:]*sp16[^:]*" nfs.lis

Cheers,
ND
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top