linux - 如何在Linux中的文件中连接具有不同模式的两行?

标签 linux unix awk sed vi

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

我可以使用 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/

    相关文章:

    macos - 如何在 macOS 上获取 awk 版本?

    c - 哪些 C 标准库函数在底层使用 malloc

    c - fork() 的执行顺序?

    linux - 如何在不压缩或归档的情况下将目录拆分为多个部分?

    regex - Linux:从行中提取和转换属性

    regex - 在文件中查找函数并打印其内容的命令

    c - 增加 FD_SETSIZE 的限制并选择

    linux - 如何在 Linux 6 上保存 Method R Workbench 的许可证 key ?

    linux - 需要与linux命令对应的Windows CMD命令

    python - 按多列唯一排序,然后合并不匹配列中的值