Replacing strings in files with bash sed or a scripting language (TCL, perl) -
i have list of c++ source files, have following structure:
// lot of stuff #include <current/parser/support/base.hpp> // ... #include <current/parser/iterators/begin.hpp> // ...
i need replace lines
#include <current/parser/support/base.hpp>
with
#include <support_base.hpp>
namely, omit current/parser
, replace separator (/
) _
. possible bash sed or scripting language?
edit: sorry, forgot mention want replace
#include <current/parser/*/*/*/*>
anything can go after current/parser
, , depth.
going tcl:
# open file reading set fin [open filein.c r] # open file write output set fout [open fileout.c w] # loop through each line while {[gets $fin line] != -1} { # check lines beginning "^#include <current/parser/" # # ^ matches beginning of line # ([^>]*) matches part after "#include <current/parser/" , stores # in variable 'match' if {[regexp {^#include <current/parser/([^>]*)>} $line - match]} { # edited line built using match above after replacing # forward slashes underscores set newline "#include <[string map {/ _} $match]>" } else { set newline $line } # put output file puts $fout $newline } # close channels close $fin close $fout
output provided input:
// lot of stuff #include <support_base.hpp> // ... #include <iterators_begin.hpp> // ...
demo on codepad (i edited code bit since can't have channel open read/write in files there)
Comments
Post a Comment