Search IP address and replace with strings | 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.

Search IP address and replace with strings

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-12-2013
sanzee007 sanzee007 is offline
Registered User
 
Join Date: Apr 2013
Last Activity: 8 September 2014, 8:42 AM EDT
Posts: 22
Thanks: 1
Thanked 0 Times in 0 Posts
Search IP address and replace with strings

Hi All,
How can I find (pattern search with grep/awk/sed) all files containing any IP address pattern in one directory hierarchy and in its sub-directories. The files can contains more than 4 octets in addition to the IP address as shown below. IP address can contain any octet combinations like-
10.132.8.234.12345 ->here first 4 numbers is the ip address, it means the IP address will have a word boundary whenever the first 4 octets are present.
123.12.35.156 etc..

Also I have one array containing unique IP addresses. I want to replace each IP address in files in a directory with different letter for each unique IP, like below-
ip_address1 - 10.232.12.156 -> XX.XXX.XX.XXX
ip_address2 - 10.28.9.136 -> YY.YY.Y.YYY
ip_address3 - 9.10.132.29 -> S.SS.SSS.SS (with some other letter and likewise..)
The above ip addresses are only for demonstration purposes here.It may be valid or invalid. Also each IP address should be replaced with equal number of letter X or Y or some other letter. The logic should work in korn shell on both Linux and Solaris OS.
Some sample logic will be like-

Code:
for file in directory
        do
                for str in ${ipaddr[*]} # ip address array 
                do
                        #replace the ip address 'str' in 'file' with unique letter for each unique IP as mentioned above
                        # some grep / awk or other pattern matching for IP search and replace logic here
                        ;
                done
        done

Sorry for this broken script syntax as I am not an advanced user with scripting knowledge.
If awk/nawk/gawk is used, please tell me what is the exact syntax to work it both on linux and Solaris OS.

Thanks in advance for your precious inputs.
Sponsored Links
    #2  
Old 05-13-2013
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 22 September 2014, 5:23 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,413
Thanks: 8
Thanked 540 Times in 518 Posts
Does the replavement need to mimic the numbers with equal numbers of letters, or is S.S.S.S sufficient? You might put your directories and letters in a two column list file for processing:
Code:
while read dir ltr ; do
...
done <config_file

Sponsored Links
    #3  
Old 05-14-2013
sanzee007 sanzee007 is offline
Registered User
 
Join Date: Apr 2013
Last Activity: 8 September 2014, 8:42 AM EDT
Posts: 22
Thanks: 1
Thanked 0 Times in 0 Posts
Hi DGPickett,
Yes, the replaced IP string should contain equal number of letter for equal number of digits, like-
12.56.123.9 ->XX.XX.XXX.X
128.32.56.110 -> YYY.YY.YY.YYY

thanks for the reply.
    #4  
Old 05-14-2013
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 22 September 2014, 5:23 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,413
Thanks: 8
Thanked 540 Times in 518 Posts
That's the hard part, using most tools. I think you could do it in multiple passes, one to find all the IP addresses to replace, create an after string for each, and then reprocess using a realtively straightforward replace, perhaps in sed 's/\<12\.56\.123\.9\>/XX.XX.XXX.X/g'. Writing a script that writes a script and runs it is level 200 progamming. I call this "lining up your ducks".
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
Search between two search strings and print the value onesuri Shell Programming and Scripting 5 08-01-2013 03:18 AM
Search & Replace: Multiple Strings / Multiple Files spacegoose Shell Programming and Scripting 2 10-16-2012 08:17 PM
Search replace strings between single quotes mnmonu Shell Programming and Scripting 6 07-25-2012 03:58 AM
Search replace strings between single quotes in a text file Hiano Shell Programming and Scripting 7 07-15-2011 10:35 AM
Help with sed search&replace between two strings angrybb Shell Programming and Scripting 2 07-26-2010 06:28 AM



All times are GMT -4. The time now is 04:20 AM.