![tail+grep+cut 조합이 작동하지 않는 이유는 무엇입니까? [복사]](https://linux55.com/image/114505/tail%2Bgrep%2Bcut%20%EC%A1%B0%ED%95%A9%EC%9D%B4%20%EC%9E%91%EB%8F%99%ED%95%98%EC%A7%80%20%EC%95%8A%EB%8A%94%20%EC%9D%B4%EC%9C%A0%EB%8A%94%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C%3F%20%5B%EB%B3%B5%EC%82%AC%5D.png)
로그를 추적하는 동안 일부 로그를 grep하고 줄이 너무 길기 때문에 각 줄의 일부만 인쇄하고 싶습니다.
내가 사용하고 있는 조합이 왜 작동하지 않는지 이해할 수 없습니다. 절차는 다음과 같습니다.
echo 'Jun 29 16:27:00 someip rails[pid]: Killed a transaction
Jun 29 16:28:00 someip rails[pid]: Killed a transaction' > /tmp/test
# Shows the lines
tail -f /tmp/test | grep Killed
# Shows the lines
grep Killed /tmp/test | cut -c -43
# Shows the lines
cat /tmp/test | grep Killed | cut -c -43
# Doesn't show them
tail -f /tmp/test | grep Killed | cut -c -43
마지막 조합이 작동하지 않는 이유는 무엇입니까?
답변1
댓글에서 지적했듯이 문제는 버퍼링으로 인해 발생합니다. 당신이 할 수 있는 일은 stdbuf
표시된 대로 명령을 실행하는 것입니다 .여기.
stdbuf -i0 -o0 -e0 tail -f /tmp/test | grep Killed | cut -c -43
grep
그런데 아마도 및 파이프를 제거 cut
하고 다음을 사용할 수 있습니다.
stdbuf -i0 -o0 -e0 tail -f /tmp/test | awk '/Killed/{print substr($0,length($0)-43)}'