how to replace the next string after match (every) two blank lines?

852 Views Asked by At

is there a way to do this kind of substitution in Awk, sed, ...?

I have a text file with sections divived into two blank lines;

   section1_name_x
   dklfjsdklfjsldfjsl


   section2_name_x
   dlskfjsdklfjsldkjflkj


   section_name_X
   dfsdjfksdfsdf

I would to replace every "section_name_x" by "#section_name_x", this is, how to replace the next string after match (every) two blank lines?

Thanks,

Steve,

3

There are 3 best solutions below

0
On

hm....

Given your example data why not just

sed 's/^section[0-9]*_name.*/#/' file > newFile && mv newFile file

some seds support sed -i OR sed -i"" to overwrite the existing file, avoiding the && mv ... shown above.

The reg ex says, section must be at the beginning of the line, and can optionally contain a number or NO number at all.

IHTH

4
On

In gawk you can use the RT builtin variable:

gawk '{$1="#"$1; print $0 RT}' RS='\n\n' file

* Update *

Thanks to @EdMorton I realized that my first version was incorrect. What happens:

  • Assigning to $1 causes the record to be rebuildt, which is not good in this cases since any sequence of white space is replaced by a single space between fields, and by the null string in the beginning and at the end of the record.
  • Using print adds an additional newline to the output.

The correct version:

gawk '{printf "%s", "#" $0 RT}' RS='\n\n\n' file
0
On
awk '
    (NR==1 || blank==2) && $1 ~ /^section/ {sub(/section/, "#&")}
    { 
        print
        if (length) 
            blank = 0
        else
            blank ++
    }
' file
   #section1_name_x
   dklfjsdklfjsldfjsl


   #section2_name_x
   dlskfjsdklfjsldkjflkj


   #section_name_X
   dfsdjfksdfsdf