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
How're you invoking the script? Please post the exact details. I assume it's something like ./script.sh fig_icwb.txt , right?
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'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.
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"