regex - Inserting text below and above a matched pattern -
first of all, such wonderful community have!! have been benefited great knowledge shared here on stackoverflow.
coming problem facing:
i have bunch of files(about 200) in files wanted search pattern(multi-line) , if pattern matches, want add text above , below pattern.
e.g
file1.cpp
#ifndef file1_h #define file1_h #ifndef parent1_h #include "parent1.h" #endif #ifndef sibling_h #include "sibling.h" #endif #ifndef parent2_h #include "parent2.h" #endif class file1 { }; #endif
in file wanted add #ifndef noparent
above #ifndef parent1_h
, #endif
below #endif
right below parent1.h
.
i want same thing #ifndef parent2_h
so output like:
#ifndef file1_h #define file1_h #ifndef noparent #ifndef parent1_h #include "parent1.h" #endif #endif #ifndef sibling_h #include "sibling.h" #endif #ifndef noparent #ifndef parent2_h #include "parent2.h" #endif #endif class file1 { }; #endif
i have list of such matches. e.g here searching parent1_h, parent2_h etc have more grandparent1_h, greatgrandparent_h etc
so essentially, approach thinking is, search input symbols(parent1_h
etc) in files , if match found, add text(#ifndef noparent
) above , #endif
below.
input symbols many , files in replace.
can please me script using sed/awk/perl. or other language/script(bash etc) great!
i novice user of sed/awk/perl use help
thanks lot :-)
best regards, marc
edit: did not read correctly request. seems give correct o/p
awk '/parent1_h/ {print "#ifndef noparent" rs $0;f=1} /#endif/ && f {print $0;f=0} !/parent1_h/' file
Comments
Post a Comment