sed는 호스트 IP 주소 이전의 모든 것을 삭제합니다.

sed는 호스트 IP 주소 이전의 모든 것을 삭제합니다.

localhost의 IP 주소 앞의 각 줄을 제거하고 싶습니다. 다음을 통해 호스트의 IP 주소를 성공적으로 쿼리했습니다.

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

이제 sed를 통해 이를 달성하는 방법을 찾고 있습니다. 예(작동하지 않음):

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

내 명령을 sed에 어떻게 포함시킬 수 있나요?

답변1

당신이 원하는

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

답변2

귀하의 awk 예를 바탕으로 원하는 솔루션은 다음과 같습니다.

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

grep그런데 파이핑과 관련된 명령 목록을 작성할 때마다 다음을 수행합니다 awk.

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

관련 정보