bash 명령 파이프 라인에서 출력을 수정하는 방법


10

예를 들어, 몇 가지 명령에서 몇 줄을 얻었습니다.

$ some-command
John
Bob
Lucy

이제 출력을 수정하는 연결 명령을 추가하고 싶습니다.

$ some-command | other-command
Hi John Bye
Hi Bob Bye
Hi Lucy Bye

쓰는 방법 other-command? (나는 bash의 초보자입니다)

답변:


16

어 wk

$ some-command | awk '{print "Hi "$1" Bye"}'

sed

$ some-command | sed 's/\(.*\)/Hi \1 Bye/'

사용 awk:

$ echo -e "John\nBob\nLucy" | awk '{print "Hi "$1" Bye"}'
Hi John Bye
Hi Bob Bye
Hi Lucy Bye

사용 sed:

$ echo -e "John\nBob\nLucy" | sed 's/\(.*\)/Hi \1 Bye/'
Hi John Bye
Hi Bob Bye
Hi Lucy Bye

당신은 잊었다some-command | paste -d\ <(printf '%s\n' Hi Hi Hi) - <(printf '%s\n' why Why WHY??)
kojiro

@kojiro- paste오늘 은 느낌이 들지 않았습니다. 8-)
slm

5

아래 코드는 한 줄씩 읽고 variable에 저장합니다 LINE. 루프 내에서 각 라인은 "Hi"및 "Bye"를 추가하여 표준 출력으로 다시 쓰여집니다.

#!/bin/bash

while read LINE ; do
   echo "Hi $LINE Bye"  
done

4

배쉬 while 루프 및 파이프 :

echo -e "John\nBob\nLucy" | while read n; do echo "hi $n bye"; done
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.