[英]How to pass output of grep to sed?
我有這樣的命令:
cat error | grep -o [0-9]
這是僅打印號碼,如2
, 30
等。 現在我希望將這個號碼傳遞給sed
。
就像是 :
cat error | grep -o [0-9] | sed -n '$OutPutFromGrep,$OutPutFromGrepp'
有可能這樣做嗎?
我是shell腳本的新手。 提前致謝
如果打算打印grep
返回的行,生成sed
腳本可能是要走的路:
grep -E -o '[0-9]+' error | sed 's/$/p/' | sed -f - error
您可能正在尋找xargs
,尤其是-I
選項:
themel@eristoteles:~$ xargs -I FOO echo once FOO, twice FOO
hi
once hi, twice hi
there
once there, twice there
你的例子:
themel@eristoteles:~$ cat error
error in line 123
error in line 234
errors in line 345 and 346
themel@eristoteles:~$ grep -o '[0-9]*' < error | xargs -I OutPutFromGrep echo sed -n 'OutPutFromGrep,OutPutFromGrepp'
sed -n 123,123p
sed -n 234,234p
sed -n 345,345p
sed -n 346,346p
對於實際使用,您可能希望傳遞sed
輸入文件並刪除echo
。
(順便說一句,修正了你的UUOC 。)
是的,您可以將輸出從grep傳遞給sed。
請注意,為了匹配整數,您需要使用[0-9] * *,而不僅僅是[0-9],它只匹配一個數字。
另請注意,您應該使用雙引號來擴展變量(在sed參數中),並且您似乎在第二個變量名中有拼寫錯誤。
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.