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

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

Post by John Sava » Mon, 10 Mar 1997 04:00:00



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

 
 
 

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

Post by Al A » Thu, 13 Mar 1997 04:00:00


makes you appreciate sed over its dad, ed

sed "=" d | sed "N;s/\n/   /"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    for a 1stop sed needs, join seders, the informal sed mailing list.      
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                        sed informal mailing list

                                ----

                           3 sed web pages

http://www.wollery.demon.co.uk   seders grab bag (seders official web page)
                                                 hosted by a seder, Casper.
http://www.math.fu-berlin.de/~guckes/sed/       run by a seder, Herr Guckes
http://www.math.fu-berlin.de/~leitner/sed/     run by a seder, Herr Leitner
http://www.dbnet.ece.ntua.gr/~george/#seders  seder, engineer, Dr2b Yiorgos

                                ----

                       my favourite DOS/UNIX sed :

 ftp://uiarchive.uiuc.edu/pub/systems/pc/simtelnet/msdos/txtutl/sed15.zip
 ftp://uiarchive.uiuc.edu/pub/systems/pc/simtelnet/msdos/txtutl/sed15x...

Directory: /pub/systems/pc/simtelnet/msdos/txtutl/
Filename    Type Length  Date    Description
===============================================================================
sed15.zip    B    62082  910930  Unix-compatible streaming editor v1.5 TC src
sed15x.zip   B    20300  910930  Unix-compatible streaming editor v1.5 EXE/docs

              sed15.zip has C source, compilable for UNIX.

             Also, check simtel for u-sedit. has nice docs.

                                ----

                        alt.comp.editors.batch            

             seders news group (much more than just  sed),
                 started by a seder, PhD2b, engineer
                         Yiorgos Adamopoulos.    

          most newsfeeds do not carry alt.comp.editors.batch
          but everyone can get it via  dejanews (and others)
  you can even follow up, reply to the poster, etc.  dejanews is great

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--
=-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
al aab, seders moderator                                      sed u soon
               it is not zat we do not see the  s o l u t i o n          
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+

 
 
 

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

Post by Scott Ha » Wed, 19 Mar 1997 04:00:00



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

Is there any reason why sed or ed is desired?  How about using pr?  As in:

        pr -e -n -t

(extand tabs to spaces, add line numbering, no headers or footers).  PR is
useful in doing all sorts of formatting -- including page numbering, page
headers, offset by a number of spaces, and so on.

Or how about "cat -n"?

--
"It was the year of hope.  It was the year of pain.       | Scott G. Hall
 It was the year of great joy."  (Babylon-5 opener)       | Lucent Technologies
----------------------------------------------------------| Bell Labs - BCS