Home > Net >  Comment out lines between two different patterns within a file
Comment out lines between two different patterns within a file

Time:12-02

I want to insert a # at the beginning of each line within a file. But only to those lines between a section:

################## PATTERN #1 ##############
blah 1243234
blah asdfsyxfvb
blah asdfasdfg
################## PATTERN #2 ##############

-->

################## PATTERN #1 ##############
#blah 1243234
#blah asdfsyxfvb
#blah asdfasdfg
################## PATTERN #2 ##############

I got it almost with my limited knowledge about awk and sed. But I'm struggling to merge the output into my file.

awk '/################## PATTERN #1/{flag=1;next}/################## PATTERN #2/{flag=0}flag' myinputfile | sed 's/^/#/'

Thanks in advance for helping me out.

P. S. As I'm new here: If someone has an idea for a better title/tags please let me know and I will change it.

CodePudding user response:

This might be what you want:

$ awk '/PATTERN #2/{f=0} f{$0="#" $0} /PATTERN #1/{f=1} 1' file
################## PATTERN #1 ##############
#blah 1243234
#blah asdfsyxfvb
#blah asdfasdfg
################## PATTERN #2 ##############

Obviously adjust the "pattern" delimiters to be whatever you want.

CodePudding user response:

Assuming the second pattern always exists, a sed one-liner might do the job:

sed '/PATTERN #1/,/PATTERN #2/s/^/#/' file

CodePudding user response:

Your code might be reworked to do what you wants. Lets file.txt content be

################## PATTERN #1 ##############
blah 1243234
blah asdfsyxfvb
blah asdfasdfg
################## PATTERN #2 ##############

then

awk '/################## PATTERN #1/{flag=1;print;next}/################## PATTERN #2/{flag=0;print;next}{print (flag?"#":"") $0}' file.txt

output

################## PATTERN #1 ##############
#blah 1243234
#blah asdfsyxfvb
#blah asdfasdfg
################## PATTERN #2 ##############

Explanation: for line with PATTERN #1 set flag to 0, print line, go to next line, for line with PATTERN #2 set flag to 1, print line, go to next line, for every other line if flag is true (1) then prepend line with # and print, if flag is false (0) then prepend line with empty string (i.e. do not change) and print. I used ternary operator (condition?valueiftrue:valueiffalse) to select string suitable to flag value

(tested in gawk 4.2.1)

CodePudding user response:

This might work for you (GNU sed):

sed '/PATTERN #1/{:a;N;/PATTERN #2/!ba;s/^[^#]\?/#&/mg}' file

Gather up lines between PATTERN #1 and PATTERN #2 and insert # at the front of lines that do not have them.

N.B. This will only update a file if both patterns are present.


To amend the least lines between PATTERN #1 and PATTERN #2 i.e. PATTERN #1 occurs again before PATTERN #2 the lines from the first occurrence of PATTERN #1 will not be commented, use:

 sed '/PATTERN #1/{:a;N;/\n.*PATTERN #1/{h;s/\(.*\)\n.*/\1/p;g;s/.*\n//;ba}
      /PATTERN #2/!ba;s/^[^#]\?/#&/mg}' file
  • Related