ตัวอย่างเช่นฉันได้รับคำสั่งจากบางบรรทัด
$ some-command
John
Bob
Lucy
ตอนนี้ฉันต้องการเพิ่มคำสั่ง chaining ที่แก้ไขเอาต์พุต
$ some-command | other-command
Hi John Bye
Hi Bob Bye
Hi Lucy Bye
จะเขียนยังother-commandไงดี? (ฉันเป็นสามเณรในทุบตี)
ตัวอย่างเช่นฉันได้รับคำสั่งจากบางบรรทัด
$ some-command
John
Bob
Lucy
ตอนนี้ฉันต้องการเพิ่มคำสั่ง chaining ที่แก้ไขเอาต์พุต
$ some-command | other-command
Hi John Bye
Hi Bob Bye
Hi Lucy Bye
จะเขียนยังother-commandไงดี? (ฉันเป็นสามเณรในทุบตี)
คำตอบ:
awk
$ 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
pasteวันนี้ขอบคุณ 8-)
โค้ดข้างล่างนี้อ่านหลายบรรทัด, LINEเก็บไว้ในตัวแปร ภายในลูปแต่ละบรรทัดจะถูกเขียนกลับไปที่เอาต์พุตมาตรฐานด้วยการเพิ่ม "Hi" และ "Bye"
#!/bin/bash
while read LINE ; do
echo "Hi $LINE Bye"
done
ทุบตีในขณะที่ห่วงและท่อ:
echo -e "John\nBob\nLucy" | while read n; do echo "hi $n bye"; done
some-command | paste -d\ <(printf '%s\n' Hi Hi Hi) - <(printf '%s\n' why Why WHY??)