Sed - comment of the corresponding line and x lines after it

I need help using sed to comment out the corresponding lines and the 4 lines that follow it. in a text file.

my text file looks like this:

[myprocess-a] property1=1 property2=2 property3=3 property4=4 [anotherprocess-b] property1=gffgg property3=gjdl property2=red property4=djfjf [myprocess-b] property1=1 property4=4 property2=2 property3=3 

I want the C # prefix to all lines with the text '[myprocess' and 4 lines that follow it the expected result:

 #[myprocess-a] #property1=1 #property2=2 #property3=3 #property4=4 [anotherprocess-b] property1=gffgg property3=gjdl property2=red property4=djfjf #[myprocess-b] #property1=1 #property4=4 #property2=2 #property3=3 

Thanks so much for your help in this.

+8
sed text-manipulation
source share
3 answers

You can do this by applying a regular expression to a set of strings:

 sed -e '/myprocess/,+4 s/^/#/' 

This corresponds to lines with "myprocess" and 4 lines after them. For these 4 lines, he then inserts the line '#' at the beginning of the line.

(I think it could be a GNU extension - it's not in any of the cheats "sed one liner" that I know)

+13
source share
 sed '/\[myprocess/ { N;N;N;N; s/^/#/gm }' input_file 
+2
source share

Using string concatenation and default actions in awk .
http://www.gnu.org/software/gawk/manual/html_node/Concatenation.html

 awk '/myprocess/{f=1} f>5{f=0} f{f++; $0="#" $0} 1' foo.txt 

or if the block always ends with an empty string

 awk '/myprocess/{f=1} !NF{f=0} f{$0="#" $0} 1' foo.txt 
+1
source share

All Articles