Linux Shell script to change grep for a string, then comment out 2 lines above and 1 below the string found in the file

1

Looking for some help on this task. I have tried several shell scripts to do this, but not sure I am approaching this problem right. I have a file that contains several blocks of data like this:

multipath {

wwid 360000970000195701574533030394530

alias DG_cln_DRPCU04_Oberon_IX_disk59

}

multipath {

wwid 360000970000195701574533030394531

alias DG_cln_DRPCU04_Oberon_IX_disk60
                }

multipath {

wwid 360000970000195701574533030394532

alias DG_cln_DRPCU04_Oberon_IX_disk61

}

multipath {

wwid 360000970000195701574533030394533

alias DG_cln_DRPCU04_Oberon_IX_disk62

}

multipath {

wwid 360000970000195701574533030394534

alias DG_cln_DRPCU04_Oberon_IX_disk63

}

multipath {

wwid 360000970000195701574533030394535

alias DG_cln_DRPCU04_Oberon_IX_disk6

}

multipath {

wwid 360000970000195701574533030394536

alias DG_cln_DRPCU04_Oberon_IX_disk64

}

I want to comment out only specific blocks. I want to change only the lines in the block that I have greped for.

Example:

grep -B 2 -A 1 DG_cln_DRPCU04_Oberon.

It would return every block that contains DG_cln_DRPCU04_Oberon. I then want to comment out that entire block of 4 lines, for each unique block that is returned. Is there an easy way to do this? Thanks for any pointers of suggestions on how to approach this problem.

Mike Marsh

Posted 2016-06-09T20:15:10.330

Reputation: 11

So when you use grep just add -n options to get the line number after this you can get line number which you need to comment out. So you can use this number in sed to change it to /*blabla*/. I think this approach will help you – makgun – 2016-06-09T21:44:08.977

Answers

0

You could do it in awk. Put the following script in a file and then call awk -v pattern=disk60 -f script_file data_file:

found { buf = buf "\n" $0 }
/multipath *\{/ { buf = $0; found = 1 }
!found { print }
$0 ~ pattern { matched = 1 }
/\}/ {
    if (matched) {
        gsub(/\n/, "\n#", buf);
        buf = "#" buf;
    }
    print buf "\n";
    buf = "";
    found = matched = 0;
}

This works as follows: If the line contains multipath and an opening curly bracket, start a new buffer, and set the variable found. As long as found is true, add the line to the buffer. If found is not true, print the line immediately, since we are outside of an interesting block. If the line matches the pattern you are looking for (disk60 in my example), set the variable matched. Finally, if the line contains a closing curly bracket, check whether matched is true. If so, comment out the lines in the buffer. Print the buffer and reset the variables.

Michael Vehrs

Posted 2016-06-09T20:15:10.330

Reputation: 255

Hi Micheal, Sorry for taking so long to get back this, got tied up on another project. Your solution works great for finding and commenting out the multipath lines, but there is other text at the beginning and end of the file, that needs to be kept in the file, your solution seems to remove that information. Is there a way to run your awk script and not remove the other data. If needed, I can pasted more of the file in this comment area I think. – Mike Marsh – 2016-08-18T20:19:17.117

What is it that you want to do, exactly? What does the other text look like? – Michael Vehrs – 2016-08-22T06:38:53.590

Here is an example file: – Mike Marsh – 2016-08-22T18:00:05.553

Sorry couldn't put the file in here, need to keep the top of the file and bottom of the file contents and make changes to the multipath lines. – Mike Marsh – 2016-08-22T18:10:53.083

I have added an additional rule. – Michael Vehrs – 2016-08-23T06:20:25.790

Wow, that is working great, with one small exception, At the end of the file, The data that is leftover in the buffer, gets printed in between each line of information that isn't related to the Multipath stanza's. Is there a way to empty the buffer after each time the buffer is printed? – Mike Marsh – 2016-08-23T14:36:08.147

That is surprising. I have added a statement to clear the buffer after it is printed. – Michael Vehrs – 2016-08-24T11:46:50.697

Michael, Thank you, It is working perfectly now. You are definitely an awk guru. This has also helped me to get a better understanding of awk. It is a fantastic tool, Hope to be as good at it as you are someday. – Mike Marsh – 2016-08-24T13:59:40.147