![](/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?
[英]SED: pasting lines between two patterns > sed skips the next pattern match
我有以下情况
sed -n '/*SET_PART/,/*/p' Inputfile > Outputfile
因此,我正在搜索以包含“*SET_PART”的行开头并以包含“*”的行结尾的段落。 (在我的场景中,“*”的出现始终是新段落的标志)
示例:输入文件
*SET_PART_COLLECT_TITLE
Gesamtfahrzeug
101
1005043 1005049 1005101 1005102 1005103 1005200 1005300 1005400
1005504 1005601 1005700 1005800 1005900 1006000 1006100 1006101
1006102 1006103 1006200 1006201 1006202 1006203 1006400 1006401
1006402 1006500 1006600 1006700 1006800 1006900 1007000 1007100
1007200 1007201 1007202 1007203 1007204 1007300 1007301 1007302
1007303 1007304 1007305 1007306 1007307 1007308 1007309 1007310
1007311 1007312 1007313 1007314 1007400 1007500 1007600 1007700
1007800 1007801 1015700 1015800
*Keyword
202
1000000 1002002 1002312 1002313 1002323 1002701
*SET_PART_COLLECT_TITLE
Verbindungstechnik_Slave
201
1000000 1002002 1002312 1002313 1002323 1002701
*SET_PART_LIST_TITLE
Bord_stein_hi_Forcetransducer
3 0. 0. 0. 0.
3
*SET_PART_COLLECT_TITLE
*SET_PART_LIST_TITLE
Fahrbahn_Forcetransducer
1
1
*END
使用当前命令行我得到:输出文件
*SET_PART_COLLECT_TITLE
Gesamtfahrzeug
101
1005043 1005049 1005101 1005102 1005103 1005200 1005300 1005400
1005504 1005601 1005700 1005800 1005900 1006000 1006100 1006101
1006102 1006103 1006200 1006201 1006202 1006203 1006400 1006401
1006402 1006500 1006600 1006700 1006800 1006900 1007000 1007100
1007200 1007201 1007202 1007203 1007204 1007300 1007301 1007302
1007303 1007304 1007305 1007306 1007307 1007308 1007309 1007310
1007311 1007312 1007313 1007314 1007400 1007500 1007600 1007700
1007800 1007801 1015700 1015800
*Keyword
*SET_PART_COLLECT_TITLE
Verbindungstechnik_Slave
201
1000000 1002002 1002312 1002313 1002323 1002701
*SET_PART_LIST_TITLE
*SET_PART_COLLECT_TITLE
*SET_PART_LIST_TITLE
所需的 Output 将是:(所有以“*SET_PART”开头的段落。)
*SET_PART_COLLECT_TITLE
Gesamtfahrzeug
101
1005043 1005049 1005101 1005102 1005103 1005200 1005300 1005400
1005504 1005601 1005700 1005800 1005900 1006000 1006100 1006101
1006102 1006103 1006200 1006201 1006202 1006203 1006400 1006401
1006402 1006500 1006600 1006700 1006800 1006900 1007000 1007100
1007200 1007201 1007202 1007203 1007204 1007300 1007301 1007302
1007303 1007304 1007305 1007306 1007307 1007308 1007309 1007310
1007311 1007312 1007313 1007314 1007400 1007500 1007600 1007700
1007800 1007801 1015700 1015800
*SET_PART_COLLECT_TITLE
Verbindungstechnik_Slave
201
1000000 1002002 1002312 1002313 1002323 1002701
*SET_PART_LIST_TITLE
Bord_stein_hi_Forcetransducer
3 0. 0. 0. 0.
3
*SET_PART_COLLECT_TITLE
*SET_PART_LIST_TITLE
Fahrbahn_Forcetransducer
1
1
换句话说:我想匹配下一次出现的“*”来匹配,但只打印到它出现之前的那一行。 而出现“*”的行应该是搜索下一个匹配“*SET_PART”的行。 这是为了防止连续出现多个“*SET_PART”段落。
如果相关,我使用 Gnome 版本 3.32.2。
对于这种情况,有没有比 sed 更好的工具?
谢谢您的帮助
这可能对你有用(GNU sed):
sed -n '/SET_PART/{:a;p;n;/SET_PART/ba;/*/d;ba}' file
这本质上是一个过滤操作,因此使用-n
关闭隐式打印。
找到包含SET_PART
的行并开始循环。
打印当前行,然后用下一行替换它。
如果该行包含SET_PART
重新启动循环。
如果该行包含*
,则将其删除并有效地结束循环。
否则重复循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.