简体   繁体   中英

Using sed to print the lines starting with two different patterns

我已经在使用以下sed命令来打印以某个单词(例如A)开头的行,但是,无法弄清楚如何使用此命令来打印以A或B开头的行。

sed -n '/A/p' 1.txt > 2.txt

Just add an alternative:

sed -n '/A\|B/p' 1.txt > 2.txt

Note that this does not only print lines starting with A or B, but lines containing A or B. To make it really match at the beginning of a line, you have to add the anchor ^ :

sed -n '/^A\|^B/p' 1.txt > 2.txt

which is equivalent to

sed '/^A\|^B/!d' 1.txt > 2.txt

which means "Delete lines that do not start with A or B".

这可能对您有用:

sed -n '/^[AB]/p' 1.txt > 2.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM