我的文件是:
abc
123
xyz
abc
675
xyz
我想提取:
abc
123
xyz
(123 可以是任何东西,重点是我想要第一次出现)
我尝试使用这个:
sed -n '/abc/,/xyz/p' filename
但这给了我所有的例子。我怎样才能得到第一个?
您能否尝试按照所示示例进行编写和测试?
awk '/abc/{found=1} found; /xyz/ && found{exit}' Input_file
或者按照埃德先生的评论,为了提高效率,请尝试以下操作。
awk '/abc/{found=1} found{print; if (/xyz/) exit}' Input_file
解释:对上述内容添加详细解释。
awk ' ##Starting awk program from here.
/abc/{ ##checking condition if a line has abc in it then do following.
found=1 ##Setting found here.
}
found; ##Checking condition if found is SET then print that line.
/xyz/ && found{ ##Checking if xyz found in line and found is SET then do following.
exit ##exit program from here.
}
' Input_file ##Mentioning Input_file name here.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)