[英]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?
[英]How can I join two lines with different patterns in file in linux?
我想將文件中的行加入兩行。 例如,如下所示,我想加入分別有諺語和作者姓名的第一行和第二行。 我想加入一個文件的所有類似事件。
我可以通過使用 Shift+J 手動加入它們,但幾乎有 10000 行,而且很難做到。
所以我想搜索並加入所有在行首有一個數字的行,然后是一個點,然后是一個空格,然后是文本..^[0-9]*。 (118. ) 和下一行在行首沒有數字。 所以加入他們。
我到處搜索並嘗試實現它但沒有用。
118. People don't care how much you know until they know how much they care.
John C. Maxwell
119. A life lived in fear is a life half lived. - Proverb
120. Nothing great was ever achieved without enthusiasm.
Ralph Waldo Emerson
121. Damn the torpedoes, full speed ahead. - David Farragut
122. Our lives begin to end the day we become silent about things that matter. -
Martin Luther King, Jr.
這應該這樣做:
awk '/^[0-9]+\./ { if (last) print last; last = $0; next }
{ print last, $0; last = "" }'
給定數據文件:
118. People don't care how much you know until they know how much they care.
John C. Maxwell
119. A life lived in fear is a life half lived. - Proverb
120. Nothing great was ever achieved without enthusiasm.
Ralph Waldo Emerson
121. Damn the torpedoes, full speed ahead. - David Farragut
122. Our lives begin to end the day we become silent about things that matter. -
Martin Luther King, Jr.
這會產生輸出:
118. People don't care how much you know until they know how much they care. John C. Maxwell
119. A life lived in fear is a life half lived. - Proverb
120. Nothing great was ever achieved without enthusiasm. Ralph Waldo Emerson
121. Damn the torpedoes, full speed ahead. - David Farragut
122. Our lives begin to end the day we become silent about things that matter. - Martin Luther King, Jr.
該代碼確實假設只有一個續行。 如果您可以有多個續行,那么您需要一個更復雜的腳本。
$ cat new.data
118. People don't care how much you know until they know how much they care.
John C. Maxwell
119. A life lived in fear is a life half lived. - Proverb
120. Nothing great was ever achieved without enthusiasm.
Ralph Waldo Emerson
121. Damn the torpedoes, full speed ahead. - David Farragut
122. Our lives begin to end the day we become silent about things that matter. -
Martin Luther King, Jr.
123. More than one line of data causes trouble for the basic script.
A more complex script can deal with those too. -
Jonathan Leffler
$ awk '/^[0-9]+\./ { if (last) print last; last = $0; next }
> { last = last " " $0 }
> END { if (last) print last }' new.data
118. People don't care how much you know until they know how much they care. John C. Maxwell
119. A life lived in fear is a life half lived. - Proverb
120. Nothing great was ever achieved without enthusiasm. Ralph Waldo Emerson
121. Damn the torpedoes, full speed ahead. - David Farragut
122. Our lives begin to end the day we become silent about things that matter. - Martin Luther King, Jr.
123. More than one line of data causes trouble for the basic script. A more complex script can deal with those too. - Jonathan Leffler
$
:%s/\\n\\%(\\d\\+\\. \\)\\@!
應該在 vim 中進行。 此命令適用於多個連續行,但僅刪除換行符; 它不插入空格或任何東西。
在 awk 中,“文件中沒有空行且 **first line** 存在”,還需要前導空格:
$ awk '{printf "%s%s", ($1 ~ /^ *\*\*/? (NR>1?ORS:"") : OFS), $0} END {printf ORS}' file
.
{ # finish previous with ORS if current starts with a number and output it
printf "%s%s", ($1 ~ /^ *\*\*/? (NR>1?ORS:"") : OFS), $0
}
END {printf ORS} # closing ORS
這可能對你有用(GNU sed):
sed 'N;/\n[0-9]/!s/\n//;P;D' file
閱讀兩行,如果第二行不以數字開頭,則刪除換行符。
其它的辦法:
sed 'N;s/\n\([^0-9]\)/\1/;P;D' file
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.