Solved

adding line numbers to a text file

Posted on 2008-06-13
7
682 Views
Last Modified: 2012-08-14
I need a Perl script to read an input text file and output a file with line numbers preceding
each line of the text.

1      this is a test
2      hello folks
3      this is the time to code
4     etc cetera
0
Comment
Question by:j2eemason
7 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 125 total points
ID: 21783355
perl -pe 'print "$. "' input >output
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 21784450
Just a note: there's also standard unix tool called "nl"
nl -ba /etc/passwd
0
 
LVL 10

Expert Comment

by:oleber
ID: 21787881
probably you need the \t

perl -ne 'print "$.\t$_ "' input >output
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Expert Comment

by:Fero45
ID: 23283957
#!/usr/bin/perl -w
use strict;

open(FROM, "file_from.txt") || die $!;
open(TO, ">file_to.txt") || die $!;
my $index = 1;

while(<FROM>)      {
      chomp;
      print TO $index . "\t" . $_ . "\n";
      $index++;
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 23284352
Are we trying to see how long we can make the program?
If so, we can further expand
while(<FROM>)      {
to
while( defined($_=<FROM>) )      {
and we can chop off another character only to add it back with
     chomp;
     my $end = chop;
      print TO $index . "\t" . $_ . $end. "\n";
we can also explicitly pass $_ to chomp and chop:
 chomp($_);
 my $end = chop($_);
while we're at it, we can turn
   $index++;
into
 eval{my $temp=$index; $index = $index+1; $temp}

0
 
LVL 6

Expert Comment

by:Fero45
ID: 23285016
The answers above already solved this question. My messing up with my solution among experts, for which I apology, is based on the fact that I am a beginner and I know what people of my level need.
Chapeau, Ozo. I saw a lot of your answers. To tell you the truth, I do not know why to chop off another character and add it back. I assume it is, in addition to \n, another line terminating character.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now