![](/img/trans.png)
[英]How can I search for two different patterns in two consecutive lines in a file using SED and print next 4 lines after pattern match?
[英]Using sed to print the lines starting with two different patterns
我已經在使用以下sed
命令來打印以某個單詞(例如A)開頭的行,但是,無法弄清楚如何使用此命令來打印以A或B開頭的行。
sed -n '/A/p' 1.txt > 2.txt
只需添加一個替代方案:
sed -n '/A\|B/p' 1.txt > 2.txt
請注意,這並不只以A開頭或B打印線,但含有 A或B線,以使它真正匹配一行的開頭,你必須添加錨^
:
sed -n '/^A\|^B/p' 1.txt > 2.txt
相當於
sed '/^A\|^B/!d' 1.txt > 2.txt
這意味着“刪除不以A或B開頭的行”。
這可能對您有用:
sed -n '/^[AB]/p' 1.txt > 2.txt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.