0

I have number of files and a set of find and replace text combinations.

For example, I have a set of find text and replace text like this,

  • find - "margin" replace with "new_margin"
  • find - "color" replace with "color_primary"
  • find - "xxx" replace with "zzzz"
  • find - "rre" replace with "768"

I have nearly 100 of such combinations, i need to find all "find texts" and replace it with corresponding replace text.

How can I do this easily?

Is there any tool to do this?

  • 1
    Could you please provide the input, I mean your find/replace sets. Are they saved in a file or how ?! – pLumo May 9 '19 at 7:35
1

to substitute the text inside the files in a single shot you can use sed:

sed -i -e 's@margin@new_margin' -e 's@color@color_primary@' -e 's@xxx@zzz@' -e '@rre@768@' /path/to/files/*

-i option say to sed to perform modification in file without print

-e say to sed have more string to be find and substituted

s@ says the '@' is the separator for string_to_find and replacement_string

the latest is the path where the file are, if they are all in one location you can perform this command once.

!!!Execute a backup of your file ALWAYS before make modification with sed in file directly!!!

UPDATE

Thanks to @steeldriver which says in the comment to not modify the source file with sed adding a suffix to the -i options

-i[SUFFIX], --in-place[=SUFFIX]

I.E.

sed -i_edited 's@margin@new_margin' /path/to/files/examplefile

will result with a new file modified called examplefile_edited

  • 1
    They could make a backup automatically by providing a suffix argument to the -i option – steeldriver May 9 '19 at 11:42
  • Upvoted and upflagged all around for backup suggestions. Most answers never suggest that. – WinEunuuchs2Unix May 9 '19 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.