Append to the previous line for a match

后端 未结 4 1210
隐瞒了意图╮
隐瞒了意图╮ 2021-01-23 09:34

can I use sed or awk to append to the previous line if a match is found ?

I have a file which has the format :

          INT32         


        
相关标签:
4条回答
  • 2021-01-23 09:42

    This might work for you (GNU sed):

    sed '$!N;s/\n\s*{\s*$/{/;P;D' file
    

    Explanation:

    • $!N unless the last line append the next line to the pattern space.
    • s/\n\s*{\s*$/{/ replace a linefeed followed by no or any amount of white space followed by an opening curly brace followed by no or any amount of white space to the end of the string, by an opening curly brace.
    • P print upto and including the first newline.
    • D delete upto and including the first newline (if so do not start a new cycle).
    0 讨论(0)
  • 2021-01-23 09:43

    One way using perl. I read all file in slurp mode and use a regular expression to search lines with only a curly brace and remove its leading spaces.

    perl -ne '
        do {
            local $/ = undef;
            $data = <>;
        };
        $data =~ s/\n^\s*(\{\s*)$/\1/mg;
        print $data
    ' infile
    

    Assuming infile with the content of the question, output will be:

    FSHL (const TP  Buffer){
    INT32
    FSHL_lm (const TP  Buffer)
    { WORD32 ugo = 0; ...
    
    0 讨论(0)
  • 2021-01-23 09:44

    [shyam@localhost ~]$ perl -lne 's/^/\n/ if $.>1 && /^\d+/; printf "%s",$_' appendDateText.txt

    that will work

    i/p:

    06/12/2016 20:30 Test Test Test

    TestTest

    06/12/2019 20:30 abbs abcbcb abcbc

    06/11/2016 20:30 test test

    i123312331233123312331233123312331233123312331233Test

    06/12/2016 20:30 abc

    o/p:

    06/12/2016 20:30 Test Test TestTestTest

    06/12/2019 20:30 abbs abcbcb abcbc

    06/11/2016 20:30 test ##testi123312331233123312331233123312331233123312331233Test

    0 讨论(0)
  • 2021-01-23 09:50

    One way using awk:

    awk '!(NF == 1 && $1 == "{") { if (line) print line; line = $0; next; } { sub(/^[ \t]+/, "", $0); line = line $0; } END { print line }' file.txt
    

    Or broken out on multiple lines:

    !(NF == 1 && $1 == "{") { 
        if (line) print line
        line = $0
        next
    }
    
    {
        sub(/^[ \t]+/, "", $0)
        line = line $0
    }
    
    END {
        print line
    }
    

    Results:

    INT32
              FSHL (const TP  Buffer){
              INT32
              FSHL_lm (const TP  Buffer) 
              { WORD32 ugo = 0; ...
    

    HTH

    0 讨论(0)
提交回复
热议问题