繁体   English   中英

如何在linux中的文件中加入具有不同模式的两行?

[英]How can I join two lines with different patterns in file in linux?

我想将文件中的行加入两行。 例如,如下所示,我想加入分别有谚语​​和作者姓名的第一行和第二行。 我想加入一个文件的所有类似事件。

我可以通过使用 Shift+J 手动加入它们,但几乎有 10000 行,而且很难做到。

  1. ,119., 120., 是原始源代码行号,它们也出现在行中。

所以我想搜索并加入所有在行首有一个数字的行,然后是一个点,然后是一个空格,然后是文本..^[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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM