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

时间:2016-09-11 04:49:34

标签: linux unix awk sed vi

我想在一个已经写成两行的文件中加入行。例如,如下所示,我想加入第一行和第二行,它们分别具有作者的谚语和名称。我想要在文件中加入的所有类似事件。

我可以通过使用Shift + J手动加入它们,但是有近10000行并且它变得非常困难。

  1. ,119。,120.,是原始的源行号,也在行中。
  2. 所以我想搜索并加入所有在行开头有一个数字的行,然后是一个点,然后是一个空格,然后是文本.. ^ [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.
    

4 个答案:

答案 0 :(得分:1)

这应该这样做:

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
$

答案 1 :(得分:0)

:%s/\n\%(\d\+\. \)\@!应该在vim中完成。此命令适用于多个延续行,但仅删除换行符;它不会插入空格或任何东西。

答案 2 :(得分:0)

在awk中,“文件中没有空行和**第一行**”,预计领先空间也是如此:

$ 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

答案 3 :(得分:0)

这可能适合你(GNU sed):

sed 'N;/\n[0-9]/!s/\n//;P;D' file

读取两行,如果第二行不以数字开头,请删除换行符。

另一种方式:

sed 'N;s/\n\([^0-9]\)/\1/;P;D' file