Creating a sed script to change ip addresses in a file

So I'm new to this sed command and I am trying to create a script that replaces ip addresses when I name a file but can't tweak it to work.

Here is what it looks like:

#!/bin/bash
#
file=$1
#
sed -e 's/-CPUaddr 10.30.10.166/-CPUaddr 10.30.10.151/g' -i "$file"
sed -e 's/-CPUaddr 10.30.10.167/-CPUaddr 10.30.10.152/g' -i "$file"
sed -e 's/-CPUaddr 10.30.10.170/-CPUaddr 10.30.10.153/g' -i "$file"
sed -e 's/-CPUaddr 10.30.10.171/-CPUaddr 10.30.10.154/g' -i "$file"

Here is my error:

: No such file or directoryfig_icwb.txt
: No such file or directoryfig_icwb.txt
: No such file or directoryfig_icwb.txt
: No such file or directoryfig_icwb.txt

Any help would be appreciated.

  1. How're you invoking the script? Please post the exact details. I assume it's something like ./script.sh fig_icwb.txt , right?
  2. If you did what's mentioned above, then the script will look for fig_icwb.txt in current directory, that is the directory from where script is invoked. Do you have fig_icwb.txt in the same directory as your script?

I have just been typing:

bash script.sh filename

I'm attempting to use it to go through other files (.txt, .scr, and .bat) to find the IP addresses and change them. It strikes me I should also mention I am using cygwin.

I have the script in the same directory as the files that I am trying to modify.

It probably means what it says, no such file or directory...

Have you been editing these scripts in Microsoft Notepad? That will fill them with pointless carriage returns which UNIX might take to be part of the filename.

dos2unix filename

---------- Post updated at 11:38 AM ---------- Previous update was at 11:37 AM ----------

Running sed 9 times to do 9 substitutions is overkill, by the way, since you can put them all into one.

sed 'expression1;expression2;expression3' ...

Will I be able to tell if the scripts have bad characters if I open it with vim?

Also I don't know how to use sed with multiple expressions. Your example isn't clear to me.

Thanks

If you mean vim as in vim in the shell, probably. if you mean vim as in a Windows implementation, I have no idea.

sed 's/cat/dog/g;s/lion/tiger/g;s/bear/shark/g'

Make sure your environement and your variable are correctly set
For troubleshooting purpose, if necessary, add some lines to display the values of the variables just before you use them so you can check their values.

Also note that with the following, replacement will directly apply in $file :

printf ',s/-CPUaddr 10.30.10.166/-CPUaddr 10.30.10.151/g\nw\nq\n' | ed -s "$file"
printf ',s/-CPUaddr 10.30.10.167/-CPUaddr 10.30.10.152/g\nw\nq\n' | ed -s "$file"
printf ',s/-CPUaddr 10.30.10.170/-CPUaddr 10.30.10.153/g\nw\nq\n' | ed -s "$file"
printf ',s/-CPUaddr 10.30.10.171/-CPUaddr 10.30.10.154/g\nw\nq\n' | ed -s "$file"

When I made those changes I get:

?
?
?
?

Though it looks like the changes were made. Is that return standard?

Thanks!

What gives :

ls -l "$file"

?

It will likely print ? if nothing is substituted (i.e. if the string to replace doesn't exist, or you run it for a second time).

Ahh, I think that was indeed the case. Thanks!