I am looking for a way to create a bash script, using possibly sed or awk, to search for a particular string of text, say “Deprecated from: 4.*”. If found, the script should then take the text found between two patterns, backup the text to a file, then remove the text from the original input file. In summary I am looking for a way to filter out certain deprecated portions of a very large MIB file that is structured like this one:
-- /*********************************************************************************/
-- /* MIB table for foo 'Something that was once very cool */
-- /* Valid from: 4.1.01 */
-- /* Valid to: 4.2 */
-- /* Deprecated from: 4.2 */
-- /*********************************************************************************/
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
-- /*********************************************************************************/
-- /* MIB table for bar 'Another thing that was once very cool */
-- /* Valid from: 4.2.01 */
-- /* Valid to: 4.3 */
-- /* Deprecated from: 4.3 */
-- /*********************************************************************************/
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
So, in this case let’s say I want to get rid of the section containing “Deprecated from 4.2”. I have come up with the following awk script which gets me closer:
{ a[i++ % 5 ]=$0}
/Deprecated from: 4.2/ {print a[(i-5)%5];print a[(i-4)%5];print a[(i-3)%5];print a[(i-2)%5];i=0}
/Deprecated from: 4.2/,/test/ {if($0 !~ /test/) print }
However this only works if the MIB uses the word “test” as the last part of the range to search for. In reality the end of the search range is as follows:
-- /*********************************************************************************/
What I need to do is skip the first instance of this, which occurs immediately after the line containing “Deprecated from” and continue searching on to the next occurrence.
Expected output after using the above MIB example, and removing all occurrences of 4.2, would be as follows:
-- /*********************************************************************************/
-- /* MIB table for bar 'Another thing that was once very cool */
-- /* Valid from: 4.2.01 */
-- /* Valid to: 4.3 */
-- /* Deprecated from: 4.3 */
-- /*********************************************************************************/
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
foo bar foo bar foo bar foo bar foo bar foo bar foo bar
See an example of the above code working here: http://ideone.com/bOQuK
My problem is I need to search for the closing pattern of
-- /*********************************************************************************/
Not:
-- /test/
Any ideas?
the proper usage of RS with awk could solve your problem:
see the test below:
I changed the content(foo bar) in your example to distinguish the text from which block:
run the awk line:
Note that the backup part is not included. however it is easy to be added. since in my script, the text should not be shown (need to be saved to anoter file as backup) is already marked.