Shell-Script
Sed Bash 腳本只執行 sed 的第一行
我的
bash
腳本只執行sed
. 我用來執行它的命令:sed -f sedhw.bash thesbians.txt
的內容
sedhw.bash
s/[0-9]*//g s/1908/---/g; s/1895/---/g; s/1903/---/g s/,.*//g s/^[^"]*"/"/ s/,[^",]*"/,/;s/"//g
我需要它一次執行文件中的每個命令,給我 5 個不同的輸出而不是一個輸出。
文本文件看起來像這樣
Chase,Cornelius 1943 "Chevy" Davis,Ruth 1908 "Bette" Keaton,Joseph 1895 "Buster" Stone,Emily 1988 "Emma" Lee,Shelton 1957 "Spike" Reynolds,Mary 1932 "Debbie" Crosby,Harry 1903 "Bing" Fey,Stamatina 1970 "Tina"
正如@roaima和@kusalananda已經評論的那樣,你
sedhw.bash
是一個sedfile。
sed -f sedhw.bash thesbians.txt
將針對文件 thesbians.txt 執行 sedhw.bash 中的所有 sed 命令相反,您要做的是循環遍歷 sedhw.bash 中的行並執行每一行以生成一個新的輸出文件:
#!/bin/bash while read num line; do sed "$line" thesbians.txt > output.${num}.txt done < <(nl sedhw.bash)
解釋一下:
nl sedhw.bash
將你的命令和“tab-separate”行號和命令編號:1 s/[0-9]*//g 2 s/1908/---/g; s/1895/---/g; s/1903/---/g 3 s/,.*//g 4 s/^[^"]*"/"/ 5 s/,[^",]*"/,/;s/"//g
<(...)
將命令輸出顯示為 while 循環的文件。循環將逐行輸入這個“文件”,
<
並將製表符分隔的欄位讀入變數“num”和“line”。
sed
執行命令“$line”並將其寫入到 sedhw.bash 中以相應行編號的文件。結果
$ for item in output*; do echo; echo "=== $item ==="; cat $item ; done === output.1.txt === Chase,Cornelius "Chevy" Davis,Ruth "Bette" Keaton,Joseph "Buster" Stone,Emily "Emma" Lee,Shelton "Spike" Reynolds,Mary "Debbie" Crosby,Harry "Bing" Fey,Stamatina "Tina" === output.2.txt === Chase,Cornelius 1943 "Chevy" Davis,Ruth --- "Bette" Keaton,Joseph --- "Buster" Stone,Emily 1988 "Emma" Lee,Shelton 1957 "Spike" Reynolds,Mary 1932 "Debbie" Crosby,Harry --- "Bing" Fey,Stamatina 1970 "Tina" === output.3.txt === Chase Davis Keaton Stone Lee Reynolds Crosby Fey === output.4.txt === "Chevy" "Bette" "Buster" "Emma" "Spike" "Debbie" "Bing" "Tina" === output.5.txt === Chase,Chevy Davis,Bette Keaton,Buster Stone,Emma Lee,Spike Reynolds,Debbie Crosby,Bing Fey,Tina