I have a file named l.txt which have follwoing data, first name and last name. When I pass the first name, output should be last name. Below query is working for me .
l.txt
name Tony Mcgill
name Jag John
name Jagmohan Singh
It works well for name tony. but when I search for jag it gives two results, John & Singh. But I need John only. How can I achieve this.
grep -e '^'"name jag" l.txt | awk '{print $3}'
I am using this command.
You can use grep -w
to match a word:
grep -iw 'tony' file
name Tony Mcgill
Alternatively use word boundary in your grep
:
grep -i '\<tony\>' file
OR:
grep -i '\btony\b' file
You can also do everything in awk :
awk '$2~/\<Jag\>/ {print $3}' l.txt
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.