![나는 grep을 사용하는 방법을 배우고 있는데 이것을 발견했습니다. 어떻게 작동하나요? [폐쇄]](https://linux55.com/image/187485/%EB%82%98%EB%8A%94%20grep%EC%9D%84%20%EC%82%AC%EC%9A%A9%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95%EC%9D%84%20%EB%B0%B0%EC%9A%B0%EA%B3%A0%20%EC%9E%88%EB%8A%94%EB%8D%B0%20%EC%9D%B4%EA%B2%83%EC%9D%84%20%EB%B0%9C%EA%B2%AC%ED%96%88%EC%8A%B5%EB%8B%88%EB%8B%A4.%20%EC%96%B4%EB%96%BB%EA%B2%8C%20%EC%9E%91%EB%8F%99%ED%95%98%EB%82%98%EC%9A%94%3F%20%5B%ED%8F%90%EC%87%84%5D.png)
어떻게 될까요?
grep '#' input.txt | tail -n
나한테 줄래? 나는 이것에 익숙하지 않으며 함께 사용 grep
하면 tail
어떤 일이 일어나는지 알 수 없습니다 .
답변1
grep '#' input.txt
이 명령은 파일에서 텍스트를 검색합니다. 이 경우 # input.txt 파일에서 검색하고 모든 항목이 화면에 인쇄됩니다.
그런 다음 파이프 기호를 사용했습니다. 이는 출력을 인쇄하지 않고 파이프 기호 뒤에 작성된 다음 명령에 출력을 전달한다는 의미입니다.
그래서
tail -n
tail -n은 맨 아래에서 언급한 숫자로 줄 수를 인쇄하기 위해 숫자가 필요하기 때문에 올바르지 않습니다. 즉, tail -n 2 마지막 2줄을 인쇄합니다.
이 예를 들어보면 다음 내용이 기록된 input.txt라는 파일이 있습니다.
[root@localhost student]# cat input.txt
# this is a new line comment 1
this is not a comment line 1
this is not a comment line 2
# this is a new line comment 2
# this is a new line comment 3
# this is a new line comment 4
# this is a new line comment 5
# this is a new line comment 6
# this is a new line comment 7
# this is a new line comment 8
# this is a new line comment 9
# this is a new line comment 10
this is not a comment line 3
this is not a comment line 4
this is not a comment line 5
명령을 실행하면
[root@localhost student]# grep '#' input.txt | tail -n 2
# this is a new line comment 9
# this is a new line comment 10
파일에서 문자를 검색하여 10줄 정도를 찾은 다음 추가로 필터링하고 tail -n 2를 작성하여 결과의 마지막 2줄을 인쇄합니다.
이제 그것이 당신에게 분명해지기를 바랍니다