LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-21-2010, 11:39 AM   #1
kishorebabue
LQ Newbie
 
Registered: Jun 2010
Posts: 5

Rep: Reputation: 0
Perl Script to replace the number from text file


Hi,
Can anyone help me out on creating a new perl script which replace IP address from the text file. eg. If in a file, we found any word like 11.222.333.44 then it has to be replaced to XX.XXX.333.44
 
Old 06-21-2010, 11:50 AM   #2
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by kishorebabue View Post
Hi,
Can anyone help me out on creating a new perl script which replace IP address from the text file. eg. If in a file, we found any word like 11.222.333.44 then it has to be replaced to XX.XXX.333.44
you do not really need a perl script for this.
sed can do this quite easily. if you must use perl, a one liner is sufficient.

Code:
sed -i.bak -e's#a\.b#c\.d#  filename
--


Code:
perl -pi.bak -e's#a\.b#c\.d# filename
 
1 members found this post helpful.
Old 06-24-2010, 10:25 AM   #3
kishorebabue
LQ Newbie
 
Registered: Jun 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by centosboy View Post
you do not really need a perl script for this.
sed can do this quite easily. if you must use perl, a one liner is sufficient.

Code:
sed -i.bak -e's#a\.b#c\.d#  filename
--


Code:
perl -pi.bak -e's#a\.b#c\.d# filename

Thanks for the command. But this doesnt work as per criteria.
The log file conatins different ip addresses and that to be replaced from nnn.nn.nn.nnn to XXX.XX.nn.nnn so this to be done in perl script using loop constraints.
 
Old 06-24-2010, 11:00 AM   #4
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
tell me exactly what needs doing and i'll write it if i get a chance - or give you pointers
 
Old 06-24-2010, 03:27 PM   #5
kishorebabue
LQ Newbie
 
Registered: Jun 2010
Posts: 5

Original Poster
Rep: Reputation: 0
i explain with simple example as below,

Input file: test_log.txt

File COntents:
This is a test file ip addr 123.23.43.333 hello.All r welocme
and another ip 34.54.222.122:1234 with the new server and so onnnn.
This is a test file ip addr 999.323.43.222 hello.All r welocme
and another ip 191.22.33.182 with the new server and so onnnn.
This is a test file ip addr 145.66.77.191 hello.All r welocme
and another ip 34.54.222.122:1234 with the new server and so onnnn.
End of file

So i need a different output file as below,
This is a test file ip addr XXX.XX.43.333 hello.All r welocme
and another ip XX.XX.222.122:1234 with the new server and so onnnn.
This is a test file ip addr XXX.XXX.43.222 hello.All r welocme
and another ip XXX.XX.33.182 with the new server and so onnnn.
This is a test file ip addr XXX.XX.77.191 hello.All r welocme
and another ip XX.XX.222.122:1234 with the new server and so onnnn.
End of file


i.e whereever i find any ip addresses(like 145.22.12.333, 22.333.44.434,......) in the input file, the first 2 contents of the id address alone to be replaced(like XXX.XX.12.333, XX.XXX.44.434, .....)
 
Old 06-24-2010, 08:09 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Well I am no perl guru but you can probably transpose the following sed into the perl example above:
Code:
sed -r 's/^([^0-9]*)[0-9]+\.[0-9]+/\1XXX\.XXX/' file
The difference here is that it is not replacing the numbers with the same number of 'X's, is this important?
 
Old 06-30-2010, 04:32 PM   #7
kishorebabue
LQ Newbie
 
Registered: Jun 2010
Posts: 5

Original Poster
Rep: Reputation: 0
im using the command, 's/(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)/XXX.XXX.$3.$4/g' to replace all ip address in a file.
but if i found any string like 222.333.444.555.444.333 also gets replaced as 222.333.XXX.XXX.444.333

222.333.444.555.444.333 = Not an valid ip address, shld not replace
222.333.444.555:3245 = ip address with port, must replaced as XXX.XXX.444.555:3245
ip/222.333.444.555 = valid ip address , must replaced as ip/XXX.XXX.444.555

How to overcome with this...
 
Old 06-30-2010, 11:56 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Well assuming the format is the same you could just put a space in front of the first set of digits, this will get rid of an invalid grouping
ie not 4 sets of numbers
 
Old 07-02-2010, 10:53 AM   #9
kishorebabue
LQ Newbie
 
Registered: Jun 2010
Posts: 5

Original Poster
Rep: Reputation: 0
but this also ignores the valid format like,
"192.22.33.232"
192.22.33.232

I just want to ignore when it is more than 4 octets, like 222.333.444.555.444.333
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl replace text in file ShaqDiesel Programming 27 08-11-2010 11:50 PM
[SOLVED] Perl replace text in file mwanakenya Programming 9 04-16-2010 04:08 AM
bash script to create text in a file or replace value of text if already exists knightto Linux - Newbie 5 09-11-2008 12:13 AM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 11:37 PM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 10:33 PM


All times are GMT -5. The time now is 02:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration