Operating System - HP-UX
1833323 Members
3353 Online
110051 Solutions
New Discussion

Re: change format of file contents

 
SOLVED
Go to solution
Tapas Jha
Valued Contributor

change format of file contents

Hi,

I have quite high number of files which contains ip address as well as URL.

File looks like below:
======================
206.251.29.10
218.102.48.99
213.158.72.99
playboy.com
admotion.com.ar
bannerlandia.com.ar
austria-chart.at
teleweb.at
admaster.candela.com.au
fairfax.com.au
=======================

Now i want to change all URL like below:

.*://.*.playboy.com/.*

And all IP address like below
.*://206\.251\.29\.10/.*

What is the best possible way to do this?
If it can be done in vi editor then also
pl. provide me syntax and also if it is possible by other way like sed, awk, perl then also it will great help to solve my problem.

regards
Tapas
Tapas Jha
5 REPLIES 5
Muthukumar_5
Honored Contributor

Re: change format of file contents

We can do this as,

#
testfile contains al your input contents.

while read line; do
echo $line | grep -q '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*'
if [[ $? -eq 0 ]]; then
echo $line | awk -F "." '{ print ".*://.*."$1"\."$2"\.$3\."$4"/.*" }'
else
echo $line | awk '{ print ".*://.*."$0"/.*" }'
fi
done < testfile

--- Output ---
.*://.*.206.251.$3.10/.*
.*://.*.218.102.$3.99/.*
.*://.*.213.158.$3.99/.*
.*://.*.playboy.com/.*
.*://.*.admotion.com.ar/.*
.*://.*.bannerlandia.com.ar/.*
.*://.*.austria-chart.at/.*
.*://.*.teleweb.at/.*
.*://.*.admaster.candela.com.au/.*
.*://.*.fairfax.com.au/.*

HTH.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor
Solution

Re: change format of file contents

Ooops.

Slight modification on ip parsing as,

# while read line; do
echo $line | grep -q '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*'
if [[ $? -eq 0 ]]; then
echo $line | awk -F "." '{ print ".*://"$1"\."$2"\.$3\."$4"/.*" }'
else
echo $line | awk '{ print ".*://.*."$0"/.*" }'
fi
done < testfile

HTH.
Easy to suggest when don't know about the problem!
Michael Schulte zur Sur
Honored Contributor

Re: change format of file contents

HI,

create
BEGIN{FS="."}
{printf ("%s",".*://");for (IA=1;IAA);print"/.*"}
END{}
as zz.awk
then
awk -f zz.awk yourfile

greetings,

Michael
BONNAFOUS Jean Marc
Trusted Contributor

Re: change format of file contents

Hi,

Make it in 2 steps:

grep "^[a-z]" |sed -e "s/^/.*:\/\/.*./" -e "s/$/\/.*/" >

grep "^[0-9]" | sed -e "s/\./\\./" -e "s/^/.*:\/\/" -e "s/$/\/.*/" >>

I suppose you can do it with awk but i don't well know this command.

Rgds
JMB
Si vous ne faites jamais de bétises, c'est que vous ne faites rien de difficile. Et ça c'est une grosse bétise.
Tapas Jha
Valued Contributor

Re: change format of file contents

Thanx a lot for your prompt reply.

Muthukumar's is working fine.
Michale in your script even in url's . is coming with \. in front and also
in begining of url it will be .*://.*.(url) and in IP it will be .*://ipwith.(dot)separated by\. and lastly /.* will be added in both.

Rgds
Tapas


Tapas Jha