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
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Vishal
  • 21

2 Answers2

1

Lets say:

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

More of this here

ss_iwe
  • 1,146
  • Thanks. It works. Can you explain command in depth? – Vishal Feb 24 '17 at 07: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. – ss_iwe Feb 24 '17 at 08: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