Operating System - HP-UX
1755136 Members
2969 Online
108830 Solutions
New Discussion юеВ

pattern search and replace

 
SOLVED
Go to solution
moonchild
Regular Advisor

pattern search and replace

Hi,

I have a text file with configuration information gathered from my systems.

I need to search those files for any IP address and delete it or mask it. I might have different IP addresses in the same file. So I am looking for a pattern like xxx.xxx.xxx.xxx to delete or maske.

How can I do that?

thank you in advance
6 REPLIES 6
IT_2007
Honored Contributor

Re: pattern search and replace

you can open the file with vi and do this way
:$s/oldstring/newstring/g

which will replace oldstring to newstring globally in the file.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: pattern search and replace

There are many ways to do this. Among them are sed, Perl, and awk. I'll use sed this time:

sed -e '/dog/d' -e '/cat/d' -e 's/cow/Giraffe/g' < infile > outfile

This would read infile and delete every line that contains 'dog' or 'cat' and substitutes "Giraffe" for every instance of "cow" and write the result to outfile.

If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: pattern search and replace

Hi:

If you want to change an address pattern, you could use:

# perl -pe 's/\Q127.0.0.1/127.0.0.2/' file

...and if you wanted to delete lines containing it:

# perl -ne 'print unless m/\Q127.0.0.1/' file

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: pattern search and replace

The simplest is to use grep to extract all the IP addresses you want:

grep -e 120.340.056.078 -e 012.012.013.103 -e 020.030.040.050 -e 015.016.170.180 your_filename

Note that this assumes you want just certain addressess and that all addresses have the same format (ie ###.###.###.###) but if the addresses have leading zeros stripped (ie, 12 rather than 012), then you'll have to match exactly what is in the file.

To change a record that matches your search criteria, you'll need to use awk or a shell or Perl script.


Bill Hassell, sysadmin
Marvin Strong
Honored Contributor

Re: pattern search and replace

This might not be the best way, but you can strip all IP patterns out of a file pretty easy with perl.

perl -p -e 's/([\d]+)\.([\d]+)\.([\d]+)\.([\d]+)//' file

if you wanted to delete them from the file completely.

perl -pi -e 's/([\d]+)\.([\d]+)\.([\d]+)\.([\d]+)//' file

moonchild
Regular Advisor

Re: pattern search and replace

Marvin,

I am new to scripting and perl.

How can I put the command you suggested in a shell script so I apply it on all the files in a directory? I am trying the following but it's not working but if I run your command on a specific file from the command line it works fine.

#!/usr/bin/sh

for FILE in `ls *.html`
do
{
perl -p -e 's/([\d]+)\.([\d]+)\.([\d]+)\.([\d]+)//' $FILE > noip-$FILE;
}
done