Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!usc!wuarchive!uunet!zephyr.ens.tek.com!tektronix!nosun!qiclab!onion!jeff From: jeff@onion.pdx.com (Jeff Beadles) Newsgroups: comp.unix.questions Subject: Re: How to add line numbers to a text file? Message-ID: <1990Dec2.055535.10392@onion.pdx.com> Date: 2 Dec 90 05:55:35 GMT References: <6826@uceng.UC.EDU> Reply-To: jeff@onion.pdx.com Lines: 30 In <6826@uceng.UC.EDU> dmocsny@minerva.che.uc.edu (Daniel Mocsny) writes: ... >My question to all of you, however, is this: faced with the need to >number a text file from a shell script, how would you do it? I'm >kind of curious to see the variety of approaches possible. Would you >use C, awk, perl, sed, sh, cat, echo, expr, and/or dd? Well, here's a couple: cat -n $file pr -n -t $file (You missed pr :-) awk '{ print NR, $0}' $file These all assume you want them numbered in decimal. To number in hex and octal use: awk '{ printf("%x %s\n", NR, $0) }' $file # Hex awk '{ printf("%o %s\n", NR, $0) }' $file # Octal Some system's don't support "cat -n". (I think SYSVR3 ??) The awk solution should work most anywhere. Have fun! -Jeff -- Jeff Beadles jeff@onion.pdx.com