0

I'm looking to delete every line before the IP address of the local host. I'm grepping the host's IP address successfully via:

grep `hostname` /etc/hosts | awk '{print $1}'

Now I'm looking how to implement this via sed. Example (not working):

cat file | sed '/echo `grep `hostname` /etc/hosts | awk '{print $1}'`/,$!d'

How to include my commands into sed?

0

You want

 sed -n "/$(hostname)/,\$ p" /etc/hosts
0

Based on your awk example, I think the solution you want is:

sed -ne "s/[[:blank:]]*$(hostname)$//p" /etc/hosts

$ hostname
foo.example.com
$ grep $(hostname) /etc/hosts
10.16.161.131         foo.example.com
$ sed -ne "s/[[:blank:]]*$(hostname)$//p" /etc/hosts
10.16.161.131

By the way, whenever you write a command list that involves grep piped to awk, they can often be combined:

$ awk "/$(hostname)/"'{print $1}' /etc/hosts
10.16.161.131

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.