Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add and delete an occurrence of characters in a file? 2

Status
Not open for further replies.

jspwoohoo

Programmer
Oct 6, 2007
6
BE
Hello,

I'm new to perl and would like to get your help.
I have a file including a list of links with 80 at the end of each link:
80
kce.fgov.be 80

I want to:
1. add http:// at the beginning of each line
2. delete 80 at the end of each line.

The result obtained should be:
All suggestions are very much appreciated.

Thanks and regards,
 
Hi Fekerke,

Thank you for your answer.
Your script only adds the string http:// between link and the number 80. What I want is to add http:// at the beginning of the link and to delete 80 at the end.

To be honest, I didn't know how to do it at all. I've gained a very basic level in Perl. I wanted to open the file, loop through the file line per line, add http:// at the beginning of each line and delete 80 at the end of each line. That was my intention but didn't know how to do it in Perl.
 
Hi

It works for me :
Code:
[blue]master #[/blue] echo '[URL unfurl="true"]www.google.com[/URL] 80
[blue]>[/blue] kce.fgov.be 80' | perl -pe 's,(\S+).*,[URL unfurl="true"]http://$1,'[/URL]
[URL unfurl="true"]http://www.google.com[/URL]
[URL unfurl="true"]http://kce.fgov.be[/URL]
It it does not work for you, probably your file format is different. Btw, you did not specified your operating system. I tested it on Linux.

Feherke.
 
Code:
perl -i -pe 's#(\S+)(\s*80)#[URL unfurl="true"]http://$1#g'[/URL] /path/to/file

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Feherke,

It is hard to say why mine works and your does not without seeing the actual data he is searching through. I just made mine a bit more explicit was all. Your regexp works fine for me when I test it with:

80
kce.fgov.be 80

Regards,
Kevin


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi Kevin,
My data is a text file (sites.txt) containing a list of sites as I said in my previous post. I got a complicated script from a colleague to extract the data. After running his script, the result obtained is the file sites.txt and on each line of the file the http:// is missing and the 80 is not moved away. That's why I want to use another script to manipulate the removal.

I don't know why it doesn't work with the script of Feherke but does work with yours.

Alice
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top