undecisive
undecisive

Reputation: 59

Bash SED to replace a line in a file

Ok so I am using Bash to call a synFlood python script for class. I want to incorporate user input into this so I used Bash (since I was not allowed to use python). I have called my source and target IP's from the user and stored them and then made new variable for the entire string inside of the python script to replace with sed.

Currently my command is working and replacing the string. But when I end the script the string I said to replace is still the original string.

EXAMPLE:

sourcepath="src = $source" #set user source IP to full path for python script
targetpath="tgt = $target" #set user target IP to full path for python script

sed -e "s/^src.*/$sourcepath/" #replace the src string with sourcepath variable
sed -e "s/^tgt.*/$targetpath/" #replace the tgt string with targetpath variable

python syn.py

Sed replaces both strings in my python file to what the user specifies; as it shows it in the terminal window (for testing anyways, final script will hide this output). But when I check the file after ending the script nothing was changed and the default values are still there.

Does sed just change the values only in the context of the script while it's running? If so, when I call the python script in the follow on line will it use the values currently in there from my sed changes or will it use the default files in the python script?

Thanks in advance for the help and clarification.

Upvotes: 1

Views: 1390

Answers (3)

fedorqui
fedorqui

Reputation: 290415

By saying

sed -e "..." file

you are performing the replacement and the output gets displayed in your screen (stdout). This is the default behaviour of sed.

If you want the file to be updated with the new content, you need to mention it. For this, you have two options:

  • In place editing

    sed -i.bak "..." file
    

this will replace the file and create a file.bak backup file with the original content. Check man sed because depending on the distributions the syntax may vary (OSX or GNU). Basically, in OSX you need to say sed -i '' if you don't want to create a backup file. See In-place edits with sed on OS X for more details.

  • Redirect to another file:

    sed "..." file > tmp_file && mv tmp_file file
    

That is, instead of printing to stdout, you print to a temporary file that then gets renamed to the original one.

Upvotes: 0

Mark Reed
Mark Reed

Reputation: 95375

The sed command doesn't modify files. It reads them in and copies them to standard output with the modifications made, but the original file is untouched.

If you have the GNU version of sed, there's a flag you can turn on to enable in-place modification: sed -i.bak s/old/new/ filename will modify the file (after creating a backup) instead of just making a copy on standard output with the changes.

If you don't have the GNU version, you can still redirect the output into a file and copy it into place:

cp -p filename filename.bak
sed -e s/old/new/ filename.bak >filename

Upvotes: 1

Etan Reisner
Etan Reisner

Reputation: 81052

That snippet is clearly not your original script as it doesn't work. Those sed commands have no data to operate on (no standard input and no filename argument).

Assuming you actually have something more like this:

sed -e "s/^src.*/$sourcepath/" syn.py #replace the src string with sourcepath variable
sed -e "s/^tgt.*/$targetpath/" syn.py #replace the tgt string with targetpath variable

then the problem is that you aren't actually changing the file on disk. You are just having sed read the file and spit the changed contents to standard output.

You need to redirect that to a file (not the same one that doesn't work) or use the -i flag to tell sed to modify the file "in-place".

Upvotes: 0

Related Questions