Solved

adding line numbers to a text file

Posted on 2008-06-13
7
739 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
Industry Leaders: 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

685 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