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!

add string at end of line perl

Status
Not open for further replies.

mouncifb

Technical User
Feb 18, 2008
5
US
I have file called ips.txt output like this:


4.4.4.2/24
3.3.3.3/26
2.2.4.5/26


the text file has more than 1000 lines I am adding "0"
at end of each line.

I wanted to look like this using perl:

4.4.4.2/24 0
3.3.3.3/26 0
2.2.4.5/26 0


ands so on, any ideas?
 
What have you tried so far?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
You have no clue what you have tried?

Just to be clear.. I'm pretty helpful.. I like to research things and hand hold people through problems, but the name of the site is tek-tips.com not tek-pleasedoalltheworkformebecauseidontwanttoeventrymyself.com

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
can someone else help with this please? I appreciate it!!
 
I agree with travs69. Also, if you don't know how to script this, simply throw this in an xls file and add a 0 in the adjacent column (copy/paste), paste back in the text file...
 
Since it's a perl forum, and not a sed forum. Here is a possible way to do it for other readers coming around.



#!/usr/bin/perl -w
use IO::File;
my $fh = IO::File->new('ips.txt','r');
die 'ips.txt: ' . $! unless($fh && $fh->opened);
while (my $line = $fh->getline) {
chomp($line);
print $line . " 0\n";
}
$fh->close;
unlink('ips.txt');


Here's a 1-liner:
cat ips.txt | perl -pe 's/[\r\n]+$/ 0\n/sg;s/ +/ /sg;'

 
I think what the OP meant to say was: somebody on another forum gave me the code.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top