0

I need to search for multiple strings in a text file, and replace with another string. I need to do this with one large search/replace rather than running search and replace many times.

Here is an example of what I need to do:

search for "apple" replace with "orange then search for "plum" replace with "kiwi" then search for "grape" replace with "watermelon"

I have all the values in a spreadsheet so I can quickly generate the proper syntax of the before and after.

I just can't figure out how to do multiple search and replaces in one command.

Any ideas would be appreciated.

(Note: I am using Text Wrangler on a Mac, but I could use other tools and have Windows also if there is a better/easier way to accomplish what I am trying to do.)

bgodfreyx
  • 1
  • 1

1 Answers1

0

here's a one liner that will do that ...

$ cat junk.txt
search for "apple" replace with "orange" then 
search for "plum" replace with "kiwi" then search 
for "grape" replace with "watermelon"
$ sed -e's/apple/orange/g' junk.txt | sed -e's/plum/kiwi/g' | sed -e's/grape/watermelon/g' > junk.txt.new ; cp junk.txt.new junk.txt
$ cat junk.txt
search for "orange" replace with "orange" then 
search for "kiwi" replace with "kiwi" then search 
for "watermelon" replace with "watermelon"
Red Cricket
  • 9,762
  • 21
  • 81
  • 166