how to insert line number in front of each line

how to insert line number in front of each line

Post by rontina_.. » Tue, 03 Oct 2000 11:27:46



Hi,

I am going to insert a line number in front of each line, for example,
the original file File1:

line a
line b
line c
....

the output File2:

1 line a
2 line b
3 line c
...

Thanks in advance.

rontina

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

how to insert line number in front of each line

Post by rontina_.. » Tue, 03 Oct 2000 12:11:27


Ok, I got it.
cat -n File1 > File2

Regards,
rontina



> Hi,

> I am going to insert a line number in front of each line, for example,
> the original file File1:

> line a
> line b
> line c
> ....

> the output File2:

> 1 line a
> 2 line b
> 3 line c
> ...

> Thanks in advance.

> rontina

> Sent via Deja.com http://www.deja.com/
> Before you buy.

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

how to insert line number in front of each line

Post by Derek M. Fly » Tue, 03 Oct 2000 12:36:50



> Ok, I got it.
> cat -n File1 > File2

Or use nl(1).
 
 
 

how to insert line number in front of each line

Post by PassenSieAu » Tue, 03 Oct 2000 04:00:00


awk '{ printf("%d   %s\n", ++i, $0) }' File1 > File2

is one way..

On the glorious day of Mon, 02 Oct 2000 02:27:46 GMT,

Quote:>Hi,

>I am going to insert a line number in front of each line, for example,
>the original file File1:

>line a
>line b
>line c
>....

>the output File2:

>1 line a
>2 line b
>3 line c
>...

>Thanks in advance.

>rontina

>Sent via Deja.com http://www.deja.com/
>Before you buy.

 
 
 

how to insert line number in front of each line

Post by Hr. Peter Krus » Tue, 03 Oct 2000 04:00:00


nl File1

is another way,

        Peter


> awk '{ printf("%d   %s\n", ++i, $0) }' File1 > File2

> is one way..

> On the glorious day of Mon, 02 Oct 2000 02:27:46 GMT,

> >Hi,

> >I am going to insert a line number in front of each line, for example,
> >the original file File1:

> >line a
> >line b
> >line c
> >....

> >the output File2:

> >1 line a
> >2 line b
> >3 line c
> >...

> >Thanks in advance.

> >rontina

> >Sent via Deja.com http://www.deja.com/
> >Before you buy.

 
 
 

how to insert line number in front of each line

Post by Glenn Wes » Wed, 04 Oct 2000 04:00:00




Quote:> awk '{ printf("%d   %s\n", ++i, $0) }' File1 > File2

> is one way..

Or even simpler:

awk '{print NR, $0}' File1 >File2

> On the glorious day of Mon, 02 Oct 2000 02:27:46 GMT,

> >Hi,

> >I am going to insert a line number in front of each line, for
example,
> >the original file File1:

> >line a
> >line b
> >line c
> >....

> >the output File2:

> >1 line a
> >2 line b
> >3 line c
> >...

> >Thanks in advance.

> >rontina

> >Sent via Deja.com http://www.deja.com/
> >Before you buy.

Sent via Deja.com http://www.deja.com/
Before you buy.
 
 
 

how to insert line number in front of each line

Post by Reinier Po » Sat, 07 Oct 2000 04:00:00



>nl File1

>is another way,

grep -n '^' File1

--
Reinier

 
 
 

how to insert line number in front of each line

Post by Michael Or » Wed, 08 Nov 2000 04:00:00


How about 'cat -n filename'?



> nl File1

> is another way,

> Peter


> > awk '{ printf("%d   %s\n", ++i, $0) }' File1 > File2

> > is one way..

> > On the glorious day of Mon, 02 Oct 2000 02:27:46 GMT,

> > >Hi,

> > >I am going to insert a line number in front of each line, for example,
> > >the original file File1:

> > >line a
> > >line b
> > >line c
> > >....

> > >the output File2:

> > >1 line a
> > >2 line b
> > >3 line c
> > >...

> > >Thanks in advance.

> > >rontina

> > >Sent via Deja.com http://www.deja.com/
> > >Before you buy.

 
 
 

1. How do I insert line numbers before each line using ed?

This thread seems to have spread to numerous additional newsgroups
since I first contributed to it.  Must be a gripping topic?  :-)

Getting back to the original request, viz., that of using ed to perform
the required numbering, it seems that only one invocation is needed:

ed - textfile > ftmp <<\+
g/^/.=
0r ftmp
kx
1,'xg/^/\
'x+1m.-1\
.-1,.j
w
q
+

This writes the line numbers to a temporary file then reads them back.
A pecularity of the g function seems to be that ed echoes to standard
output the lines that are matched. In this scheme these are unavoidably
going to be appended to the temporary file, so I've arranged that it's
the line numbers that are echoed rather than the lines of data so that
the amount of unwanted text being written is kept to a minimum. A file
residing in ram would be ideal, as /tmp sometimes is.

BTW other posters have alluded to ed solutions having been posted here
but (apart from that nightmarish-looking scheme) I've seen none and this
I attribute not to a flaky newsfeed but rather a *partial* newsfeed. :-(
And I've eschewed the n function, it being unavailable in older versions.
--
John Savage     koala ~ sydney.dialix.com.au     <-- use this address

2. Stack Size Limit???

3. Shirt

4. Sed command, Insert text in front of existing line

5. Linux + Windows 95.

6. Insert a line of text every nth line

7. network cloud traffic simulator?

8. Vi -- How to Insert a Blank Line to all existing lines

9. inserting a blank line after every line - using sed

10. Insert a line at the first line without a # in a file

11. Vi command to insert at the begin of a line for lines in a file

12. Delete a whole line with sed, then insert diff text on same line...