-1

I have file like below

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
10.120.6.19 slatbnrduva00.ad.admin slatbnrduva00
#172.25.17.75 infrbaddc01.ad.lab

Where "slatbnrduva00" is my hostname. Now i am trying to delete that line using sed as below

sed -i '/`hostname`/d' myhost

but it is not deleting that line. Please let me know how to use built in linux command in sed

Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476
iamarunk
  • 95
  • 1
  • 14

2 Answers2

2

Unix built in commands need to be expanded.

You can do :

$(hostname)

or

`hostname`

But back-tics is old and deprecated.


To expand commands in sed, you need to use double quote ". With singel quote ' no expand.

So this should be the best why to do it:

sed -i "/$(hostname)/d" file
Jotne
  • 39,326
  • 11
  • 49
  • 54
0

Use grep:

grep -vFw "$(hostname -f)" file

-v reverse the match, which would effectively remove lines containing the hostname. -F will treat the search pattern as a fixed string rather than a regular expression. Meaning the . in the hostname means a literal ., not any character. -w matches at word boundaries meaning bar.baz.com would not match foobar.baz.com

hek2mgl
  • 143,113
  • 25
  • 227
  • 253