0

I want to grep variable pattern which is in first column.

$ cat test.txt    
abc.xyz   
abc.def
pqr.tap
pqr.abc
abc.mnp
mnp.abc
abc.pqr
abc.mob

If variable pattern is abc then output should be

abc.xyz
abc.def
abc.mnp
abc.pqr
abc.mob

If variable pattern is mnp then output should be

mnp.abc

If pattern is not variable then I can do by command (For pattern mnp)

awk -F"." '{ if($1 == "mnp") print $0;}' test.txt
1
  • 2
    grep '^abc' or awk '/^abc/' Commented Feb 23, 2017 at 14:32

2 Answers 2

1

Lets say:

pt="abc"
awk -v variable="$pt" -F"." '$1 ~ variable {print $0}' test.txt 

More of this here

2
  • Thanks. It works. Can you explain command in depth? Commented Feb 24, 2017 at 7:27
  • In the command variable is an awk variable for which a shell variable pt was assigned. With filed separator . we get $1 and compare it with variable and if it matches it prints the whole line.from the file test.txt. As for why to use awk variable, refer the aforementioned link. Commented Feb 24, 2017 at 8:48
0

If you always have the same line format you can simply grep "abc." (or whatever before the point including the point):

grep abc. file.txt

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.