![](/img/trans.png)
[英]Using sed - how to replace the first occurrence of a string on the line after the first occurrence of a variable
[英]UNIX: Using egrep or sed to find the line with the first occurrence of a string?
我在一個bash shell中工作,我試圖只打印第一次出現的字符串的行。 例如,對於字符串' auir
',如果我有文件myfile.txt並且它包含:
123
asdf
4wirajw
forauir somethingelse
starcraft
mylifeforauir
auir
something else
tf.rzauir
我想輸出“ forauir somethingelse
”
到目前為止,我使用命令
sed -n '/auir/p' myfile.txt
這給了我所有這個字符串的出現。 我怎樣才能獲得' auir
'出現的第一行? 如果它只是一個命令或命令管道,那就太棒了。
非常感謝任何見解。
用這個:
grep -m1 auir myfile.txt
這個sed
命令
sed -n '/auir/p' myfile.txt | head -1
解決你的問題。
這可能對你有用:
sed '/auir/!d;q' file
要么
sed -n '/auir/{p;q}' file
sed -n -e'4s / auir / auir / p'文件
或者它可以像這樣簡單
grep auir myFile.txt|head -1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.