LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-15-2009, 08:41 AM   #1
shayno90
Member
 
Registered: Oct 2009
Distribution: Windows10 Linux Mint NST Kali CentOS
Posts: 203
Blog Entries: 3

Rep: Reputation: 24
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 194.169.240.130 | 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} 202.103.9.51:1096 -> 95.224.96.106:1434',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 09:04 AM.
 
Old 10-15-2009, 09:15 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

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

Quote:
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, 09:31 AM   #3
jstephens84
Senior Member
 
Registered: Sep 2004
Location: Nashville
Distribution: Manjaro, RHEL, CentOS
Posts: 2,098

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

Rep: Reputation: 27
Should be pretty easy. Try this:
Code:
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!
 
  


Reply

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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:36 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
Open Source Consulting | Domain Registration