簡體   English   中英

UNIX:使用egrep或sed查找第一次出現字符串的行?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM