0

we have the following /etc/hosts file

130.21.25.5 kafka01.bd-dm-kag.com
130.12.22.6 kafka02.bd-dm-kag.com
130.12.22.10 kafka03.bd-dm-kag.com

I want to use grep in order to match the kafka01 machine , as the following

machine=kafka01

grep -E '(\s$machine)[[:digit:]]+'  /etc/hosts

but grep not matched the kafka01 ( no output from grep )

we try also

 grep -E '\s$machine[[:digit:]]+'  /etc/hosts

what is wrong with my syntax?

note - perl one linear or awk can be also considered if not solution from grep

Judy
  • 1,595
  • 6
  • 19
  • 41
  • See [Difference between single and double quotes in Bash](https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash) – Jetchisel Jun 13 '21 at 10:13
  • 1) You need to enclose `$machine` into double quotes, not single quotes. 2) To get a match, the name of the machine must be behind the digits in the regex, not before. 3) If you want to select the whole IP address, the regex must also foresee the presence of dots. So use: `grep -E "[[:digit:].]+\s$machine" /etc/hosts`. – Pierre François Jun 13 '21 at 10:59
  • 1
    Running a simple, without bells and whistles grep "$machine" /etc/hosts will display the line 130.21.25.5 kafka01.bd-dm-kag.com. – Aditya Jun 13 '21 at 11:10

0 Answers0