regex - sed - Remove parts of certain strings across multiple files -
from tutorial here, learned find , replace text across multiple files linux command line. example,
find . -name "*.php" -print | xargs sed -i 's/foo/bar/g' finds instances of 'foo' in files end '.php' , replaces such instances 'bar'.
now suppose have multiple, redundant occurrences of assert something.makeassertion want replace something.makeassertion. given something can vary, how adapt script this? tried things $ capture something variable (?) haven't figured out.
superficially, use:
find . -name '*.php' -exec \ sed -i.bak -e 's/assert\(.*\.makeassertion\)/\1/g' {} + the regex drops assert when there's .makeassertion later on line. find … -exec runs sed on files finds. + indicates 'group convenient number of files single command line', rather xargs without complications. alternative using gnu find , xargs be:
find . -name '*.php' -print0 | xargs -0 sed -i.bak -e 's/assert\(.*\.makeassertion\)/\1/g' both these avoid problems spaces or newlines in file names.
Comments
Post a Comment