LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need to get this code to run through whole text file while changing a field (https://www.linuxquestions.org/questions/linux-newbie-8/need-to-get-this-code-to-run-through-whole-text-file-while-changing-a-field-762088/)

shayno90 10-15-2009 08:41 AM

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?

pixellany 10-15-2009 09:15 AM

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.

jstephens84 10-15-2009 09:31 AM

Yes the perl syntax for global change is /g. Eg
Code:

$string =~ s/regex/replacement/g;
pretty much like you would in sed.

sploot 10-15-2009 01:30 PM

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!


All times are GMT -5. The time now is 04:57 PM.