Article View: comp.editors
Article #3826Re: Adding Line Numbers to a text file
From: Moshe Zadka
Date: Tue, 22 Sep 1998 00:00
Date: Tue, 22 Sep 1998 00:00
59 lines
2264 bytes
2264 bytes
On 21 Sep 1998, Gabor wrote:
> In comp.editors, Arnzie <mkarnel@ballarat.starway.net.au> wrote :
> # Hi, I was wondering if anybody could tell me where I could find a program
> # which prints line numbers.
> # I have a text file with the code from a program I made in Borland JBuilder,
> # and I want to add line numbers to the start
> # for easy reference. If anybody knows of such a program, could they please
> # email me at mkarnel@ballarat.starway.net.au
> # Any help I recieve will be much appreciated.
> #
> # Thank you
> # Adam Arnel
>
> cat -n <file>
Or, with perl
----- Cut here -----
#!/usr/bin/perl -w
use strict;
my $i=1;
while(<>) {
print $i++, " ", $_;
}
----- Cut here -----
Or, in C
----- Cut here -----
#include <stdio.h>
int main(int argc, char **argv)
{
int i=1;
int c;
printf("%10d ", i++);
while((c=getc(stdin))!=EOF) {
putc(c, stdout);
if(c=='\n') {
printf("%10d ", i++);
}
}
}
----- Cut here --------
Or with awk
----- Cut here --------
#!/usr/bin/awk -f
{print NR, $0}
----- Cut here --------
Which do you prefer?
--
Moshe Zadka <moshez@math.huji.ac.il> | (\_/)
What's Yellow and Complete?A Bananach Space|( =(^Y^)=
Being smart means being able to count to 20| \_(m___m)
without taking off your shoes - Micky Mouse|(originally by jgs)
Technical Details
Message-ID:
<Pine.SUN.3.95-heb-2.07.980922071543.10652B-100000@sunset.ma.huji.ac.il>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<XZpN1.21$K%5.67531@nsw.nnrp.telstra.net> <slrn70dlmu.195.gabor@guava.vmunix.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 108eba,b1a82c63440172f5 X-Google-Attributes: gid108eba,public X-Google-Thread: 1018fa,b1a82c63440172f5 X-Google-Attributes: gid1018fa,public X-Google-Thread: 107393,b1a82c63440172f5 X-Google-Attributes: gid107393,public X-Google-Thread: 10bba1,b1a82c63440172f5 X-Google-Attributes: gid10bba1,public From: Moshe Zadka <moshez@math.huji.ac.il> Subject: Re: Adding Line Numbers to a text file Date: 1998/09/22 Message-ID: <Pine.SUN.3.95-heb-2.07.980922071543.10652B-100000@sunset.ma.huji.ac.il> X-Deja-AN: 393547389 References: <XZpN1.21$K%5.67531@nsw.nnrp.telstra.net> <slrn70dlmu.195.gabor@guava.vmunix.com> X-Sender: moshez@sunset.ma.huji.ac.il Content-Type: TEXT/PLAIN; charset=US-ASCII Organization: The hebrew University of Jerusalem Mime-Version: 1.0 Newsgroups: comp.editors,comp.text,fj.editor.misc,okinawa.comp.editors