Using the command SED

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!

This is one way of doing it:

sed 's/Search/Replace/' Input > Output

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:

sed 's/'"$search"'/'"$replace"'/' fileA
1 Like

1)

sed -i s/search/replace/' fileA

2) Assume that fileC, fileD contains just one string.
try this:
TO_GREP=`cat fileC`
TO_REP=`cat fileD`

sed "s/$TO_GREP/$TO_REP/" fileA
1 Like

I am working on a xp machine, and I am telneting to a linux system.

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

HTH
--ahamed

1 Like

When I do 2), can I execute the 3 lines one at a time?

What do you mean by that? They are executed one at a time right?

--ahamed

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 ----------

I have a section of text in file A

# falkdjf lkjadf lkjadf
lkajdf lkajdf lkajdf lkjadf
lkjadf 234.234.2.234
lkjlkjlk 234.234.3.234
#

I want to replace that section of text with something else. how do I do that?