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!

KSH help....cuts and greps?

Status
Not open for further replies.

AdamCoombs

Technical User
May 22, 2002
51
GB
Ok,
I have a file that looks like:
1 Bob
2 Fred
3 Barry
etc etc.
i need to search on the number and print the name..easy when there is only 9 people in the file..cos i can grep the number out and then pipe it through an awk to print $2 BUT when it get to double figures, if you grep "1" then it will also come up with 11, 12 etc etc...
cant go a fgrep -x, tried to cut the first coloum out but then dont know how to print the second field..
any ideas?

thanks
Adam
 
Code:
num=123
awk '$1=="'$num'"{print $2}' file
 
If you are using awk to print it, you could just grab the line with awk instead of grep. ie:
Code:
#!/bin/sh

echo -n "Enter a number: "
read pattern
awk "\$1 ~ /^$pattern\$/ "' { print $2 } ' /file/to/parse
Hope that helps.
 
Superb..
thanks for the help chaps..
done the job....
 
Try grep with the -w option. -w searches for the "Word".


grep -w "1" | awk '{print $2}'
 
Or this...
Code:
   grep "^1 " filename
Hope this helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top