简体   繁体   中英

Using sed, for every occurrence of a string, except the first, delete two lines

I have a bunch of text files that were generated from TN3270 screens that contain an annoying 2-line header every 24 lines. The first line of each header contains "X310A000", but I want to keep the first occurrence of the header (which is not on the first line).

I can delete all the headers with

 sed '/X310A000/{N;d}' $file

but my attempt at printing everything up to the first occurrence and then deleting the rest of the headers is not working:

sed '1,/X310A000/p;/X310A000/,$ /X310A000/{N;d}' $file
sed: -e expression #1, char 28: unknown command: `/'

What can I do?

If you want to give awk a chance then it is much easier:

awk 'index($0, "X310A000") { if (p) {getline; next} else p=1 } 1' file

This command toggles a flag p to 1 when it encounters pattern first time. Once flag is set it will skip line with the pattern and the next line from output.

为第二个范围添加其他花括号:

sed '1,/X310A000/p;/X310A000/,${/X310A000/{N;d}}' $file

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