Search code examples
linuxunixtext

How to replace rows starting with a certain character with emptly lines in text file with Linux/Unix?


I'm using Ubuntu and have a large text file where certain rows start with < character what I'd like to replace each of these to an empty row. So from this:

eeeeee
<
<
aaaa
bbbb
cccc
<
dddddd
<
ff

I want this:

eeee

aaaa
bbbb
cccc

dddddd

ff

(In case of multiple consecutive < rows only one empty rows is needed ideally)

How to perform this in command line?


Solution

  • This Perl one-liner should do what you're asking for:

    perl -ne 'if (/^</) {print "\n" if !$f; $f=1} else {$f=0; print}' tmp.txt
    

    Here it is in action:

    # cat tmp.txt
    eeeeee
    <
    <
    aaaa
    bbbb
    cccc
    <
    dddddd
    <
    ff
    # perl -ne 'if (/^</) {print "\n" if !$f; $f=1} else {$f=0; print}' tmp.txt
    eeeeee
    
    aaaa
    bbbb
    cccc
    
    dddddd
    
    ff
    

    Commented code:

    # Found a line starting with '<'
    if (/^</) {
        # Print a blank line if $f is false
        print "\n" if !$f;
        # Set $f to true so subsequent lines starting with '<' are ignored
        $f=1;
    } else {
        # Not a line starting with '<'; reset $f to false
        $f=0;
        # Print the current line
        print;
    }