Solved

adding line numbers to a text file

Posted on 2008-06-13
7
753 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Six Sigma Control Plans

691 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