[英]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.