Solved

adding line numbers to a text file

Posted on 2008-06-13
7
692 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

20 Experts available now in Live!

Get 1:1 Help Now