Solved

adding line numbers to a text file

Posted on 2008-06-13
7
745 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

742 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