Sponsored Content
Top Forums Shell Programming and Scripting Search IP address and replace with strings Post 302805997 by sanzee007 on Sunday 12th of May 2013 03:51:16 AM
Old 05-12-2013
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.
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

2. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

3. Shell Programming and Scripting

Help with sed search&replace between two strings

Hi, i read couple of threads here on forum, and googled about what bugs me, yet i still can't find solution. Problem is below. I need to change this string (with sed if it is possible): This is message text that is being quoted to look like this: This is message text that is being quotedI... (2 Replies)
Discussion started by: angrybb
2 Replies

4. Shell Programming and Scripting

Search replace strings between single quotes in a text file

Hi There... I need to serach and replace a strings in a text file. My file has; books.amazon='Let me read' and the output needed is books.amazon=NONFOUND pls if anybody know this can be done in script sed or awk.. i have a list of different strings to be repced by NONFOUND.... (7 Replies)
Discussion started by: Hiano
7 Replies

5. Shell Programming and Scripting

Search replace strings between single quotes

Hi All, I need to serach and replace a strings between single quote in a file. My file has : location='/data1/test.log' and the output needed is location='/data2/test_dir/test.log' pls if anybody know this can be done in script sed or awk. I have a script, but it's... (6 Replies)
Discussion started by: mnmonu
6 Replies

6. Shell Programming and Scripting

Search & Replace: Multiple Strings / Multiple Files

I have a list of files all over a file system e.g. /home/1/foo/bar.x /www/sites/moose/foo.txtI'm looking for strings in these files and want to replace each occurrence with a replacement string, e.g. if I find: '#@!^\&@ in any of the files I want to replace it with: 655#@11, etc. There... (2 Replies)
Discussion started by: spacegoose
2 Replies

7. Shell Programming and Scripting

Search between two search strings and print the value

Based on the forums i have tried with grep command but i am unable to get the required output. search this value /*------ If that is found then search for temp_vul and print and also search until /*------- and print new_vul Input file contains: ... (5 Replies)
Discussion started by: onesuri
5 Replies

8. Shell Programming and Scripting

Help needed with shell script to search and replace a set of strings among the set of files

Hi, I am looking for a shell script which serves the below purpose. Please find below the algorithm for the same and any help on this would be highly appreciated. 1)set of strings need to be replaced among set of files(directory may contain different types of files) 2)It should search for... (10 Replies)
Discussion started by: Amulya
10 Replies

9. Shell Programming and Scripting

How to search and lossless replace strings using sed?

Hello, I would like to replace all occurencies of long data types by others (coresponding int) using 'sed' in the extensive source code of a software package written for 32 bit CPUs. I must use regular expressions to avoid wrong replacements like s/unsigned]+long/ulong/gLeaving out... (2 Replies)
Discussion started by: Mick P. F.
2 Replies
BZGREP(1)						      General Commands Manual							 BZGREP(1)

NAME
bzgrep, bzfgrep, bzegrep - search possibly bzip2 compressed files for a regular expression SYNOPSIS
bzgrep [ grep_options ] [ -e ] pattern filename... bzegrep [ egrep_options ] [ -e ] pattern filename... bzfgrep [ fgrep_options ] [ -e ] pattern filename... DESCRIPTION
Bzgrep is used to invoke the grep on bzip2-compressed files. All options specified are passed directly to grep. If no file is specified, then the standard input is decompressed if necessary and fed to grep. Otherwise the given files are uncompressed if necessary and fed to grep. If bzgrep is invoked as bzegrep or bzfgrep then egrep or fgrep is used instead of grep. If the GREP environment variable is set, bzgrep uses it as the grep program to be invoked. For example: for sh: GREP=fgrep bzgrep string files for csh: (setenv GREP fgrep; bzgrep string files) AUTHOR
Charles Levert (charles@comm.polymtl.ca). Adapted to bzip2 by Philippe Troin <phil@fifi.org> for Debian GNU/Linux. SEE ALSO
grep(1), egrep(1), fgrep(1), bzdiff(1), bzmore(1), bzless(1), bzip2(1) BZGREP(1)
All times are GMT -4. The time now is 07:40 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy