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
  • 55,929
  • 26
  • 146
  • 227
Vishal
  • 21
  • 3

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,136
  • 6
  • 12
  • 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
Zumo de Vidrio
  • 1,703
  • 1
  • 13
  • 28