Creating a sed script to change ip addresses in a file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Creating a sed script to change ip addresses in a file

Shell Programming and Scripting


Tags
cygwin, linux, script, sed

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-15-2012
uradunce uradunce is offline
Registered User
 
Join Date: Feb 2012
Last Activity: 27 February 2012, 3:26 PM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
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:
How to use code tags

Last edited by Franklin52; 02-16-2012 at 02:41 AM.. Reason: Please use code tags for code and data samples, thank you
Sponsored Links
    #2  
Old 02-15-2012
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 26 September 2014, 11:00 PM EDT
Location: India
Posts: 1,828
Thanks: 25
Thanked 524 Times in 503 Posts
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?
Sponsored Links
    #3  
Old 02-16-2012
uradunce uradunce is offline
Registered User
 
Join Date: Feb 2012
Last Activity: 27 February 2012, 3:26 PM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
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
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 1 October 2014, 1:35 PM EDT
Location: Saskatchewan
Posts: 19,480
Thanks: 796
Thanked 3,289 Times in 3,084 Posts
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' ...

Sponsored Links
    #5  
Old 02-16-2012
uradunce uradunce is offline
Registered User
 
Join Date: Feb 2012
Last Activity: 27 February 2012, 3:26 PM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #6  
Old 02-16-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 1 October 2014, 1:35 PM EDT
Location: Saskatchewan
Posts: 19,480
Thanks: 796
Thanked 3,289 Times in 3,084 Posts
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'

Sponsored Links
    #7  
Old 02-16-2012
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 1 October 2014, 9:32 AM EDT
Location: France
Posts: 2,930
Thanks: 81
Thanked 624 Times in 596 Posts
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"

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script to get all ip addresses of servers into a file brian112 Shell Programming and Scripting 1 07-01-2010 05:42 AM
(Urgent):Creating flat file using sql script and sqlplus from UNIX Shell Script praka Shell Programming and Scripting 6 04-15-2009 06:09 AM
Ip Addresses With Hardware Change golfs4us IP Networking 4 09-28-2006 02:59 PM
How do I change IP addresses in command mode... Super.Anyak UNIX for Dummies Questions & Answers 6 06-09-2004 04:01 PM
creating virtual ip addresses rmuhammad UNIX for Dummies Questions & Answers 2 11-03-2003 09:41 AM



All times are GMT -4. The time now is 10:28 AM.