I am trying to use sed with substitute to replace a section of text in a fileA.
Question 1:
If I want to want replace the "search" string with "replace" string inside fileA. I understand that I can use
sed 's/search/replace' < fileA >fileB
I also tried to do <fileA >fileA, but it doesn't work. Why is that? I don't want an extra file, fileB.
Question 2:
My main question. Inside the comand sed 's/search/replace' < fileA >fileB, the string "search" and "replaced" are actually inside fileC and fileD. How do I write the command? Thanks!
Which system are you using? UNIX tool behave differently between systems
I think you're missing the last slash. And if your sed takes files as argument, the `<` is useless:
sed 's/search/replace/' fileA >fileB
Try -i option:
sed -i 's/search/replace/' fileA
If fileC and fileD contain multiple search & replace patterns, you need to write a script that read each line in fileC and fileD to variables and use them in your sed command:
root@bt> cat fileA
Linux Unix Solaris Windows
root@bt> cat fileC
Windows
root@bt> cat fileD
Oops
root@bt> sed "s/$(<fileC)/$(<fileD)/g" fileA
Linux Unix Solaris Oops
Storing it in fileB
root@bt> sed "s/$(<fileC)/$(<fileD)/g" fileA > fileB
root@bt> cat fileB
Oopsx Unix Solaris Oops
Modifying the fileA inline using -i option in sed
root@bt> sed -i "s/$(<fileC)/$(<fileD)/g" fileA
root@bt> cat fileA
Oopsx Unix Solaris Oops
I meant do they need to be in s script or can I just key in each line sequentially and press enter after each line?
---------- Post updated at 02:34 PM ---------- Previous update was at 01:10 PM ----------
I actually want to do a search and replace on multiple line. I want to replace one paragraph with other. Does the search criteria and replace text has to be a line?
---------- Post updated at 02:50 PM ---------- Previous update was at 02:34 PM ----------