我想将文件中已写入两行的行连接起来。例如,如下所示,我想加入第一行和第二行,分别有谚语和作者姓名。我想将所有类似的事件加入到一个文件中。
我可以使用 Shift+J 手动加入它们,但有将近 10000 行,这变得非常困难。
- ,119., 120.,是也出现在 line 中的原始源代码行号。
所以我想搜索并连接所有在行首有一个数字,然后是一个点,然后是一个空格,然后是文本的所有行.. ^[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
$
关于linux - 如何在Linux中的文件中连接具有不同模式的两行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39432967/