I need to search and replace multiple words. For example, in my file, I want to search for any name of week days that appears with a comma after it: Sat, or Sun, or Mon, or Tue, or Wed, or Thur, or Fri, to a name without comma: Saturday or Sunday or Monday or Tuesday or Wednesday or Thursday or Friday. I know how to do this one by one:

sed 's/Sat,/Saturday/g' myfile.txt > output.txt

But due to the fact that I have to check every week day in multiple files, I need to find a more efficient way to check all the names and replace them at once. I am aware this can be done through scripting but I have not worked on that before. If someone familiar with this, I can learn from it this time and do it myself next time.

  • Take a look at GNU sed's option -f and -i. See: man sed – Cyrus Jul 17 at 19:45

You can chain multiple sed expressions like this:

sed -e 's/Sat/Saturday/g' -e 's/Sun/Sunday/g' <file>

This will replace all occurrences of Sat with Saturday and all occurrences of Sun with Sunday.

  • Why do you add -e ?? – qbq Jul 17 at 20:06
  • -e means script or --expression otherwise sed will interpret the second argument as the first <file> to use and it will error with .. can't read s/Sun/Sunday/g: No such file or directory – Nicolai Fröhlich Jul 17 at 20:09
  • How to ignore case-sensitivity in sed? because the day name may or may not be capital letter. – qbq Jul 17 at 20:12
  • 1
    use /ig or /Igat the end of the expression – Nicolai Fröhlich Jul 17 at 20:17
  • Shouldn't the search text (Sat, Sun, here) have commas after them? – Jeff Schaller Jul 17 at 21:50

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.