[英]How to join several consecutive lines using one pattern for the first line and the other pattern for all following lines, preferably with sed?
我想從此示例中刪除整個部分“派生詞”,兩個都包括。 到目前為止,我已經想到了將“派生單詞:”行之后的行與該行連接並刪除的想法,但是我不能僅將以下兩行連接起來,每條文章的行數可能會有所不同。 因此,我的想法是檢查行是否與模式'^ Derived words:'相匹配,然后檢查下一行是否與模式'^ [az]'相匹配,如果為true,請合並在一起,檢查下一行...聽起來工作很完美對於Bash的if-then-else,但如果可能的話,我更喜歡純sed解決方案。
A swift event or process happens very quickly or without delay.
Our task is to challenge the UN to make a swift decision...
The police were swift to act.
Syn:
quick
Derived words:
swiftly The French have acted swiftly and decisively to protect their industries.
swiftness The secrecy and swiftness of the invasion shocked and amazed army officers.
Something that is swift moves very quickly.
With a swift movement, Matthew Jerrold sat upright.
Syn:
quick
Derived words:
swiftly ^[[0;37m...a swiftly flowing stream.
swiftness With incredible swiftness she ran down the passage.
A swift is a small bird with long curved wings.
預期成績
A swift event or process happens very quickly or without delay.
Our task is to challenge the UN to make a swift decision...
The police were swift to act.
Syn:
quick
Something that is swift moves very quickly.
With a swift movement, Matthew Jerrold sat upright.
Syn:
quick
A swift is a small bird with long curved wings.
提前致謝
這可能對您有用(GNU sed):
sed -n '/^Derived words:/{:a;n;/^\w/ba};p' file
在遇到Derived words:
時使用seds grep-like標志-n
Derived words:
繼續閱讀,直到非詞在行首匹配為止。
我發現當您要處理多行塊時,最好的工具往往是awk,例如:
awk '/^Derived words/{skip=1} /^ /{skip=0} 1{if(!skip)print}' input
A swift event or process happens very quickly or without delay.
Our task is to challenge the UN to make a swift decision...
The police were swift to act.
Syn:
quick
Something that is swift moves very quickly.
With a swift movement, Matthew Jerrold sat upright.
Syn:
quick
A swift is a small bird with long curved wings.
這應該在常規(非GNU)sed中工作。 可能有一種消除冗余模式的方法,但是我還沒有提出。
sed -e :a -e '/^Derived words:/N;s/\n[a-z]//;ta' -e 's/^Derived words:.*\n//'
運作方式如下:
但是,當它在一行的開頭遇到“派生詞:”時,在回顯之前,它會將下一行讀入模式空間並追加到“派生詞:”中,並用換行符將它們分開(N命令),自從看到“派生詞:”以來,沒有回聲。 然后,它嘗試刪除該換行符和緊隨其后的字母字符 (s命令)。
如果隨后發現一行以“ Derived words:”開頭的行,則將其刪除直到換行符並包含換行符(第二個s命令)-保留換行符之后的部分,即下一條非連續行- -呼應。 然后,它繼續處理下一行的輸入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.