![](/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.