0

I am new to linux, I have one 200 lines in a file. In that file I need to replace the specific word eg: existing word: foo New word : bar I read some blogs...i understood it can be done with sed. But I do not know how to do that with a shell script

sed 's/foo/bar/' /path to a file 

I need to write a script, I do not know how to give the file as an input or should I store in a variable and change the specific word.

The script should change the specific word as well as the file name eg: Input File name: cat home.txt (word to be replaced -->cat) OutPut Filename: Dog home.txt(Cat should be replaced with Dog)

Kindly help!

8
  • May the file also contain the string foo as a substring of a word that should not be modified, for example food, foobar or my-foo? Commented Mar 19, 2020 at 8:04
  • sed can read from standard input or take a filename as argument. These work: sed 's/foo/bar/' < "file" or sed 's/foo/bar' "file". Commented Mar 19, 2020 at 8:23
  • @Kusalananda No the file doesn't contain substring...its only foo..sed command should reolace the exact word..and it should be case senstitive Commented Mar 19, 2020 at 8:38
  • @Devon yes , it works like giving the file as an input, but in my case i need to replace in case senstitive...how can i do it with sed Commented Mar 19, 2020 at 8:41
  • Do you plan on editing the file in-place? Doing it in a script is no different than doing it on the command line. Commented Mar 19, 2020 at 8:56

1 Answer 1

0

If you want to change the string foo with bar then you could use this:

#!/bin/bash # the pattern we want to search for search="foo" # the pattern we want to replace our search pattern with replace="bar" # my file my_file="/path/to/file" # generate a new file name if our search-pattern is contained in the filename my_new_file="$(echo ${my_file} | sed "s/${search}/${replace}/")" # replace all occurrences of our search pattern with the replace pattern sed -i "s/${search}/${replace}/g" "${my_file}" # rename the file to the new filename mv "${my_file}" "${my_new_file}" 

Please be aware that if the search pattern matches parts of a word those parts are also substituted e.g.:

"I have a caterpillar."

with a search string of "cat" and a replace string of "dog", would become

"I have a dogerpillar."

Avoiding this a unfortunately not fully trivial.

6
  • Thank u for your answer...Is it possible to make case sensitive? foo -->bar Foo--Bar Commented Mar 19, 2020 at 12:51
  • am getting the error while reneming the file "No such file or directory" Can u pls help me Commented Mar 19, 2020 at 13:36
  • When you are adding the -x flag to the first line of the script (#!/bin/bash -x ) then you are able to see which commands are executed. This should give you a hint about the problem. Commented Mar 19, 2020 at 13:55
  • thank u....Could you please help me with the case senstitive... For eg: In the same command sed should be able to replace both small and caps Commented Mar 19, 2020 at 13:57
  • for eg: foo -- >bar Foo -->Bar. How to do that Commented Mar 19, 2020 at 13:57

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.