![sed는 호스트 IP 주소 이전의 모든 것을 삭제합니다.](https://linux55.com/image/156957/sed%EB%8A%94%20%ED%98%B8%EC%8A%A4%ED%8A%B8%20IP%20%EC%A3%BC%EC%86%8C%20%EC%9D%B4%EC%A0%84%EC%9D%98%20%EB%AA%A8%EB%93%A0%20%EA%B2%83%EC%9D%84%20%EC%82%AD%EC%A0%9C%ED%95%A9%EB%8B%88%EB%8B%A4..png)
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