Creating a sed script to change ip addresses in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating a sed script to change ip addresses in a file
# 1  
Old 02-15-2012
Question 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:
Code:
#!/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:
Code:
: 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.

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 02-16-2012 at 03:41 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-15-2012
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?
# 3  
Old 02-16-2012
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.
# 4  
Old 02-16-2012
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.
Code:
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.

Code:
sed 'expression1;expression2;expression3' ...

# 5  
Old 02-16-2012
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
# 6  
Old 02-16-2012
If you mean vim as in vim in the shell, probably. if you mean vim as in a Windows implementation, I have no idea.

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

# 7  
Old 02-16-2012
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 :

Code:
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"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk or sed script to count number of occurrences and creating an average

Hi Friends , I am having one problem as stated file . Having an input CSV file as shown in the code U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_2_/Q,1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,0... (4 Replies)
Discussion started by: kshitij
4 Replies

2. UNIX for Beginners Questions & Answers

Using sed to change file into an awk script

Hi I want to use sed to change a text files input into an awk script. For example if the input says " chord -- english " I want to change this using sed 's/pattern 1 /pattern 2 /'g filename but I don't understand how to use part of the pattern 1 to input that into pattern 2 . Like after... (10 Replies)
Discussion started by: enforcer
10 Replies

3. UNIX for Beginners Questions & Answers

File name change with sed

I have a bunch of text files like this: Sample_S1_L001_R1.txt Sample_S10_L001_R1.txt Sample_S11_L001_R1.txt I am using the following script to add a 0 to those files with a single digit after the S: ls *.txt | sed 's/\(.*_S\)\(_.*\)/mv & \10\2/' | sh And then the following script to... (4 Replies)
Discussion started by: Xterra
4 Replies

4. UNIX for Advanced & Expert Users

Change user while creating spool file

Hi, I have a script which generate a HTML file from a unix user. Script spool the data extracted from DB using sqlplus. Problem is html file which gets generated is automatically assigned/owned with oracle user so my unix user just having read only permission to that HTML file. So I want... (1 Reply)
Discussion started by: sv0081493
1 Replies

5. UNIX for Dummies Questions & Answers

sed script to change timecode string

Need some help. I need to run a script to modify a csv file. Here is an example off a few lines from the csv 98M-01.WAV,98M,01,00:00:49,01:07:36:00,"MIX",,"BOOM-MKH50",,,,,,,,,,"", 98L-01.WAV,98L,01,00:00:51,01:01:45:00,"MIX",,"BOOM-MKH50",,,,,,,,,,"", I need a sed script to find all... (4 Replies)
Discussion started by: Vrc2250
4 Replies

6. Shell Programming and Scripting

script to get all ip addresses of servers into a file

Hi all i need to create a script that pings every server in my range (0-254) adn then returns the values to a file? can anyone please help. i am working in the tcsh ( and yes i know how to ping ) but i dont know how to ping them all in one script without copying and pasting a 254 times? ... (1 Reply)
Discussion started by: brian112
1 Replies

7. IP Networking

Ip Addresses With Hardware Change

I have 2 identical printers with the same IP on my network. One is only used as a backup for the primary printer and both are never on line at the same time. The goal was to always keep only 1 on and have an use the other printer as an immediate backup if necessary. But when I switch the cable I... (4 Replies)
Discussion started by: golfs4us
4 Replies

8. Shell Programming and Scripting

How to change this file with SED?

I have a file like below: I want to delete the rows which begining with "BC" "CD" and "TY" . then change every fields into one row like this at last delete the head words : USing awk to change it is not hard. I want to know how to do this work using SED ? Thank you! ... (4 Replies)
Discussion started by: bejgirl
4 Replies

9. UNIX for Dummies Questions & Answers

How do I change IP addresses in command mode...

How do I change IP addresses in command mode? i need to assign custom (non DHCP) addresses from the command line. Actually I also need to know how to change the subnet mask, the gateway and the primary, secondary, ... dns servers. (6 Replies)
Discussion started by: Super.Anyak
6 Replies

10. UNIX for Dummies Questions & Answers

creating virtual ip addresses

i am running solaris 9 i now how to create virtual ip address but how do i keep them so when the server reboots they are still there?...THANX (2 Replies)
Discussion started by: rmuhammad
2 Replies
Login or Register to Ask a Question