Removing new line after a particular text via bash/awk/sed/perl

前端 未结 8 1066
迷失自我
迷失自我 2021-01-02 00:22

I would like to remove all the newline character that occurs after a partiular string and replace it with a tab space. Say for instance my sample.txt is as follows



        
8条回答
  •  一个人的身影
    2021-01-02 00:31

    The sed version looks like

    #!/bin/sed -f
    /foo/{
    N
    s/\n/\t/
    }
    

    If this is all you want to do in your sed command, you can simplify the above to a one-liner:

    sed -e '/foo/N;y/\n/\t/'  

    Explanation: N appends a newline and the next line of input; y/\n/\t replaces all newlines with tabs, or s/\n/\t/ replaces the first newline with a tab.

    Note that since sed is line-oriented, the newlines in the input are considered line-separators rather than as part of any line (this is in contrast e.g. to Perl).

提交回复
热议问题