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?


You want

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

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

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

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

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

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.