Reputation: 14146
Suppose that I have a file like this:
tst.txt
fName1 lName1-a 222
fname1 lName1-b 22
fName1 lName1 2
And I want to get the 3rd column only for "fName1 lName1", using this command:
var=`grep -i -w "fName1 lName1" tst.txt`
However this returns me every line that starts with "fName1 lName1", how can I look for the exact match?
Upvotes: 2
Views: 106
Reputation: 1718
Here you go:
#!/bin/bash
var=$(grep -Po '(?<=fName1 lName1 ).+' tst.txt)
echo $var
The trick is to use the o
option of the grep
command. The P
option tells the interpreter to use Perl-compatible regular expression syntax when parsing the pattern.
Upvotes: 2
Reputation: 6960
you can try this method:
grep -i -E "^fName1 lName1\s" tst.txt | cut -f3,3- -d ' '
But you must be sure that line starts with fName1
and you have space after lName1
.
Upvotes: 0