Visit The New, Modern Unix Linux Community


How to replace all ip addresses in all files in directory in ksh?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace all ip addresses in all files in directory in ksh?
# 1  
How to replace all ip addresses in all files in directory in ksh?

Hi All,
I have a directory containing a lot of files and I want to search all files which contains IP addresses pattern and want to replace those ip addresses with equal number of, say, some character, 'x'. So after replacement the file should not contain any IP address. Like
10.122.53.5 -> xx.xxx.xx.x
12.176.254.24 -> xx.xxx.xxx.xx
the replacement should not change anything other than those IP address numbers, retaining all white spaces, newlines etc. intact.

for fname in $(find "$DIR" -type f -print)
do
#for each file replace the IP numbers here in the file
done

How can I achieve this using commands like grep/sed/awk/tr or may be combining use of all these commands?

Please do care about the different shells working behavior like something may work in bash, but may not work in korn shell. I am using ksh for this.

Thanks in advance for the replies.
sanzee
# 2  
If you are ok with a Perl solution:

Code:
$ cat file
abc 10.122.53
10.122.53.5
efg 25.a23.33.44
12.176.254.24

Perl command:
Code:
$ perl -i -ple 'if (/(.*)((\d{1,3}\.){3}\d{1,3})(.*)/){$x=$2;$x=~s/\d/x/g;$_=$1.$x.$3;}' file

File after the command:
Code:
$ cat file
abc 10.122.53
x.xxx.xx.x
efg 25.a23.33.44
x.xxx.xxx.xx


Guru.
# 3  
Thanks guru for the reply,
sorry i can;t go with perl. However the replacement should be like -
Code:
$ cat file
abc 10.122.53
10.122.53.5
efg 25.a23.33.44
12.176.254.24

output-
Code:
$ cat file
abc 10.122.53
xx.xxx.xx.x
efg 25.a23.33.44
xx.xxx.xxx.xx

So it will be equal number of x's for equal number of digits in ip address.
# 4  
How about this
Code:
 perl -nle 'if (/([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])/) {($f=$&)=~ s/[0-9]/x/g; print $f;}else{print}' Filename


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #22
Difficulty: Easy
RAM is considered volatile memory in a computer system.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check for files and move it to another directory - ksh

I'm trying to wirte ksh script for given requirement, but i unable to achive it. In dir1 directory I need to check for the files which suffixed with .csv or .txt, If there is no files, then i need to exit. If any files found I need to move the each file found to dir2 directory. I have to repeat... (4 Replies)
Discussion started by: Kayal
4 Replies

2. Shell Programming and Scripting

Grep for all ip addresses mentioned in all files in a Directory

I wish to traverse all files and folders under a given directory say "/tmp/configuration" and for all ip address mentioned therein. I tried find ./ -type f | xargs grep "*.*.*.*" but it does not populated the correct results. Can you please suggest. (1 Reply)
Discussion started by: mohtashims
1 Replies

3. Shell Programming and Scripting

Replace directory paths in multiple files at once

I need to update about 2400 files in a directory subtree, with a new directory path inside the files I need to change this occurence in all files: /d2/R12AB/VIS/apps/tech_st/10.1.2 with this: /u01/PROD/apps/apps_st/10.1.3 I know how to change single words using "find . -type f -print0 |... (6 Replies)
Discussion started by: wicus
6 Replies

4. Shell Programming and Scripting

how to replace words in mutiple files under the same directory

I would like to get help to find how to replace word in files from command line instead of to vi to each file. This is the command i am running now. grep <old word> * vi (file with the word found in it) 1,$s/<old word>/<new word>/g It would very helpful if I can combine these in one... (2 Replies)
Discussion started by: ywu081006
2 Replies

5. Shell Programming and Scripting

how to find files and replace them in a directory in Shell scripting

I have a directory /java/unix/data In data directory i have so many files from which i want to find some files who look alike below.(there are number of such below such files as well different files too in the data directory) -68395#svg.xml -56789#ghi.xml -67894#gjk.org -56734#gil.txt I... (6 Replies)
Discussion started by: pratima.kumari
6 Replies

6. Shell Programming and Scripting

Replace a string in all files under a directory and its subdirectories

Hello Friends, I've been trying to write a script which finds a string and change it with another string. For this i want to search all files (with its arguments) under a spesific directory and its subdirectories. For example lets assume i want to replace an IP= 192.168.0.4 with another... (4 Replies)
Discussion started by: EAGL€
4 Replies

7. Shell Programming and Scripting

how to do search and replace on text files in directory

I was google searching and found Perl as a command line utility tool This almost solves my problem: find . | xargs perl -p -i.old -e 's/oldstring/newstring/g' I think this would create a new file for every file in my directory tree. Most of my files will not contain oldstring and I... (1 Reply)
Discussion started by: siegfried
1 Replies

8. Shell Programming and Scripting

find and replace string in a directory files

Hi, I have a directory has DIR1 and the D1 directory has 200+ files. I want change the string from "Bangalore" to "Bangaluru" in all files in the D1 directory. Thanks (2 Replies)
Discussion started by: koti_rama
2 Replies

9. Shell Programming and Scripting

Need to search and replace in multiple files in directory hierarchy

Hello all I need to search and replace in multiple files that are in directory hierarchy Im using the : find . -name "*.dsp" -print | xargs grep -n -o Test.lib" , I like to be able to replace every instance of Test.lib with empty space . how can I write one liner that does this ? (3 Replies)
Discussion started by: umen
3 Replies

10. Shell Programming and Scripting

ksh: How to get latest file from a list of files in a directory

Hi, I need to get the latest file from a list of files in a particular directory. Please could anyone help me out to get the file. Thank you, - Jay. (1 Reply)
Discussion started by: Jayathirtha
1 Replies

Featured Tech Videos