Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 10-15-2009, 09:41 AM   #1
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 187
Blog Entries: 2

Rep: Reputation: 21
Need to get this code to run through whole text file while changing a field

Hi, I have code to change to convert IP addresses to integers but you have to give it an address

echo | perl -ne 's/(\d+)\.(\d+)\.(\d+)\.(\d+)/$1<<24|$2<<16|$3<<8|$4/e;print'
this gives something like 3265917058

However I want it to covert these 2 IP addresses that are in the same line (there are multipe lines of data the same in the same file) as below

'Mop-21050905','auth','info','info','26','2009-10-09 12:45:20','snort','snort[4574]: [1:2050:14] SQL version overflow attempt [Classification: Attempted Administrator Privilege Gain] [Priority: 1]: {UDP} ->',8764657

change it to
'Mop-21050905','auth','info','info','26','2009-10-09 12:45:20','snort','snort[4574]: [1:2050:14] SQL version overflow attempt [Classification: Attempted Administrator Privilege Gain] [Priority: 1]: {UDP} 329292829:1096 -> 2888901919:1434',8764657

otherwise i have to go through each individual line to do it, there are too many!!

I think sed would do it, but want to have the changes kept in the same file.

Any ideas anybody?

Last edited by shayno90; 10-15-2009 at 10:04 AM.
Old 10-15-2009, 10:15 AM   #2
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 735Reputation: 735Reputation: 735Reputation: 735Reputation: 735Reputation: 735Reputation: 735
The equivalent SED command uses the "g" flag to change all occurences on a line. I would think PERL would have something similar.

I think sed would do it, but want to have the changes kept in the same file.
SED useds the "-i" switch to cause the changes to be written back to the same file. It also has an option to make a backup of the original.
Old 10-15-2009, 10:31 AM   #3
Senior Member
Registered: Sep 2004
Location: Nashville
Distribution: Manjaro, RHEL, CentOS
Posts: 2,096

Rep: Reputation: 102Reputation: 102
Yes the perl syntax for global change is /g. Eg
$string =~ s/regex/replacement/g;
pretty much like you would in sed.
Old 10-15-2009, 02:30 PM   #4
Registered: Sep 2009
Location: Phoenix, AZ
Distribution: Gentoo, Debian, Ubuntu
Posts: 121

Rep: Reputation: 27
Should be pretty easy. Try this:
sed -i 's/\.//g' $filename
switches all periods in the file to nothing. If all that is in your input file are lines like the one you posted, then it should only edit IP addresses. If you have more lines, then grep for only those with IP addresses and feed them back into the file in order with a for loop.

Good luck!


code, convert, ip, sed

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
Simple Code to convert a field in the same file shayno90 Linux - Newbie 6 10-16-2009 04:36 AM
remove the extra numeric field in a text file powah Programming 13 01-08-2008 09:24 PM
how not to print the 4th field from a text file with six fields livetoday Red Hat 3 10-02-2007 02:19 PM
Help with changing the formatting of a text file. zackarya Programming 4 05-06-2006 02:47 PM
Changing file-type of a text file olspookishmagus Linux - General 3 05-03-2006 02:00 AM

All times are GMT -5. The time now is 11:55 AM.

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