brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (12/01/90)
In article <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? grep -n .* ---Dan
schriste@uceng.UC.EDU (Steven V. Christensen) (12/01/90)
dmocsny@minerva.che.uc.edu (Daniel Mocsny) writes: >I need to add line numbers to a text file. >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? Under SunOS I would type: cat -n filename > wherever Or under most any Unix: awk -e '{print NR, " ",$0}' filename > wherever Happy hacking! Steven >-- >Dan Mocsny Snail: >Internet: dmocsny@minerva.che.uc.edu Dept. of Chemical Engng. M.L. 171 > dmocsny@uceng.uc.edu University of Cincinnati >513/751-6824 (home) 513/556-2007 (lab) Cincinnati, Ohio 45221-0171 -- Steven V. Christensen U.C. College of Eng. schriste@uceng.uc.edu For the adventurous: svc@elf0.uucp
rhoward@msd.gatech.edu (Robert L. Howard) (12/01/90)
In <15248:Nov3018:01:4490@kramden.acf.nyu.edu> brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes: >In article <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? >grep -n .* Huh? This is not even a complete answer.... Try: cat -n <infile >outfile (It's got to be faster than grep too) Robert -- | Robert L. Howard | Georgia Tech Research Institute | | rhoward@msd.gatech.edu | MATD Laboratory | | (404) 528-7165 | Atlanta, Georgia 30332 | | UUCP: ...!{allegra,amd,hplabs,ut-ngp}!gatech!msd!rhoward | ------------------------------------------------------------------------- | "They [La Prensa] accused us of suppressing freedom of | | expression. This was a lie and we could not let them | | publish it." -- Nelba Blandon, Director of Censorship |
brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (12/01/90)
In article <rhoward.659993691@romeo> rhoward@msd.gatech.edu (Robert L. Howard) writes: > In <15248:Nov3018:01:4490@kramden.acf.nyu.edu> brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes: > >In article <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? > >grep -n .* > Huh? This is not even a complete answer.... Of course it is. Whatever your shell is, you want to exec grep with arguments -n and .*. Of course, in most shells you'll have to quote the asterisk. By assumption, the input is a text file. Why is more information necessary? > cat -n <infile >outfile > (It's got to be faster than grep too) Possibly, but I find its output format more difficult to use directly as input to other programs. (Yeah, that's it: echo 'set nu' >> ~/.exinit. :-) ) ---Dan
tchrist@convex.COM (Tom Christiansen) (12/01/90)
cat -n < infile > outfile you could also use grep -n, sed, awk, or perl, with only slightly more complexity. --tom
terryl@sail.LABS.TEK.COM (12/01/90)
In article <rhoward.659993691@romeo> rhoward@msd.gatech.edu (Robert L. Howard) writes: +In <15248:Nov3018:01:4490@kramden.acf.nyu.edu> brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes: + +>In article <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? + +>grep -n .* + +Huh? This is not even a complete answer.... BUZZ!!! Thank you for playing, but you're wrong. According to man grep(1) -n Each line is preceded by its relative line number in the file. Although Dan should have quoted the asterisk (tsk, tsk, tsk, Dan!!! (-:) +Try: + +cat -n <infile >outfile + +(It's got to be faster than grep too) Maybe yes, maybe no. Most of the time spent in most *grep's is spent generating the FSA for the pattern matching, not the actual pattern matching itself.
gwyn@smoke.brl.mil (Doug Gwyn) (12/01/90)
In article <rhoward.659993691@romeo> rhoward@msd.gatech.edu (Robert L. Howard) writes: >In <15248:Nov3018:01:4490@kramden.acf.nyu.edu> brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes: >>In article <6826@uceng.UC.EDU> dmocsny@minerva.che.uc.edu (Daniel Mocsny) writes: >>> number a text file from a shell script, how would you do it? I'm >>grep -n .* >Huh? This is not even a complete answer.... Actually, it's a pretty good answer, because it immediately leads one to appreciate that the numbering can be tailored somewhat so that, for instance, only non-blank lines are numbered, merely by altering the search pattern. 0 >Try: >>cat -n <infile >outfile >(It's got to be faster than grep too) It sure is faster: $ cat -n cat: illegal option -- n usage: cat -usvte [-|file] ... The job of "cat" is to concatenate files, not to translate them. (The "-v" option (along with "-t" and "-e") is an abomination added at UCB.) The best versions of "cat" have no options at all; they aren't needed.
jim@segue.segue.com (Jim Balter) (12/01/90)
In article <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? pr -tn filename grep -n $ filename If you simply RTFM (permuted index), you'll find nl filename on PWB/SVID based systems, and num filename (cat -n) on BSD
jay@silence.princeton.nj.us (Jay Plett) (12/02/90)
In article <14629@smoke.brl.mil>, gwyn@smoke.brl.mil (Doug Gwyn) writes: > The best versions of "cat" have no options at all; they aren't needed. The best religions have no zealots at all; they aren't needed. ...jay
jeff@onion.pdx.com (Jeff Beadles) (12/02/90)
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
ralfi@pemstgt.PEM-Stuttgart.de (Ralf Holighaus) (12/02/90)
dmocsny@minerva.che.uc.edu (Daniel Mocsny) writes: >I need to add line numbers to a text file. I am processing the text file >from a Bourne shell script. I know that I can write a C program that >reads each line of standard input with gets(), increments a line >counter, and then prints the line counter and line to standard output. >If I call the program number.c, then I can put a line in my shell >script like: > number < $TEXTFILE > $NUMBEREDFILE >I could also write a one-line awk script, but because I am much more >familiar with C than awk, almost every time I try to do something simple >in awk I wind up taking longer. >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? >-- >Dan Mocsny Snail: >Internet: dmocsny@minerva.che.uc.edu Dept. of Chemical Engng. M.L. 171 > dmocsny@uceng.uc.edu University of Cincinnati >513/751-6824 (home) 513/556-2007 (lab) Cincinnati, Ohio 45221-0171 HOW ABOUT USING THE NL COMMAND AVAILABLE ON THE UNIX SYSTEM??? rgds Ralf. -- Programmentwicklung fuer Microcomputer | Ralf U. Holighaus PO-Box 810165 Vaihinger Strasse 49 | >> PEM Support << D-7000 Stuttgart 80 West Germany | holighaus@pemstgt.PEM-Stuttgart.de VOICE: x49-711-713045 FAX: x49-721-713047 | ..!unido!pemstgt!ralfi
skwu@boulder.Colorado.EDU (WU SHI-KUEI) (12/04/90)
With System V you have several choices for numbering lines in text files, none of which use sed, awk, perl etc., etc.: 1. If the document will be formatted with n/troff, turn on line numbering with the .nm request. 2. If the document has been formatted and is paginated, 'pr -tn file' will do just fine if you don't mind having all lines numbered. 3. The line numbering filter 'nl' offers all sorts of options worth exploring. Regardless of your choice, RTFM.
chip@tct.uucp (Chip Salzenberg) (12/04/90)
For line numbering, the SVID includes a little-known, seldom-used utility called "nl" (number lines). For interactive or short-lived scripts, I use "nl -ba" for line numbering. Otherwise, I tend to write a Perl script to do the whole job. :-) -- Chip Salzenberg at Teltronics/TCT <chip@tct.uucp>, <uunet!pdn!tct!chip> "I'm really sorry I feel this need to insult some people..." -- John F. Haugh II (He thinks HE'S sorry?)
prg@mgweed.UUCP (Gunsul) (12/04/90)
Haven't seen anyone mention: pr -t -n filename Phil
exspes@gdr.bath.ac.uk (P E Smee) (12/06/90)
In article <4905@segue.segue.com> jim@segue.segue.com (Jim Balter) writes: >In article <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? > >pr -tn filename >grep -n $ filename > >If you simply RTFM (permuted index), you'll find >nl filename on PWB/SVID based systems, and >num filename (cat -n) on BSD I've gotten to feel like the poor guy who posted this has gotten an inordinate amount of flak, so I feel compelled to post. Jim, I'm not accusing you of providing the flak. I picked yours to hang off of because I wanted to agree that the permuted index is overlooked far too often, and your suggestion for looking there as a first guess is probably one of the most useful suggestions. To everyone else, note that Dan didn't say he didn't know how to do this, he said he was interested in comparing the methods used by different people. TFM doesn't help there. Also, note that the problem is system dependent anyway. (Assuming standard versions, no local or manufacturer-specific enhancements): nl is not available under BSD (the one true Unix), V7, or Xenix prior to 3.0. Further, the -p option is SysV only; depends on what you want the file numbered for, but for some purposes -p is the 'right' way. cat -n is only available in BSD. Not as much control over style as nl. grep -n is probably the most portable easy way, if you want ALL lines numbered. I don't believe you could force it into 'nl -p' behavior, since any selector used to determine which lines are to be numbered also determines which lines are output. So, not interesting for some purposes. awk if you want to do it portably, and don't do it often enough to make writing a C program worthwhile, and want something other than 'all lines numbered', awk is probably the way to go. My guess would be that any of these would be better than building a numberer from 'shell parts' (echo, etc). If I had to do it often and portably, I'd probably hack together a special purpose C program to do it, for speed as compared to grep or awk. Since I don't do it often, that's not worth it. -- Paul Smee, Computing Service, University of Bristol, Bristol BS8 1UD, UK P.Smee@bristol.ac.uk - ..!uunet!ukc!bsmail!p.smee - Tel +44 272 303132
exspes@gdr.bath.ac.uk (P E Smee) (12/06/90)
In article <6765@mgweed.UUCP> prg@mgweed.UUCP (Gunsul) writes: > >Haven't seen anyone mention: > > pr -t -n filename Another good one, though -n is only available standardly on S-III, S-V, and Xenix 3.0 and later. Not Berkeley, V7, or early Xenix. -- Paul Smee, Computing Service, University of Bristol, Bristol BS8 1UD, UK P.Smee@bristol.ac.uk - ..!uunet!ukc!bsmail!p.smee - Tel +44 272 303132
ccsdhd@gdt.bath.ac.uk (Dennis Davis) (12/06/90)
Newsgroups: comp.unix.questions Subject: Re: How to add line numbers to a text file? Summary: Expires: References: <15248:Nov3018:01:4490@kramden.acf.nyu.edu> <rhoward.659993691@romeo> <18438:Nov3021:25:2590@kramden.acf.nyu.edu> Sender: Followup-To: Distribution: Organization: Bath University Computing Services, Bath, England Keywords: In article <18438:Nov3021:25:2590@kramden.acf.nyu.edu> brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes: . . . >(Yeah, that's it: echo 'set nu' >> ~/.exinit. :-) ) > >---Dan Nice to see someone suggesting the use of my favourite editor :-) However it isn't necessary to modify your .exrc startup file. A command line of the form: ex - -R +'set nu|1,$p|q' filename should suffice. Yes, I know it suffers from the disadvantages of (a) Being slow and inefficient. (b) Will only handle a limited size of file. (c) You can't tailor the output. but, what the heck, it's different :-) -- Dennis Davis JANET: D.H.Davis@UK.AC.BATH University of Bath UUCP: ...!mcsun!ukc!gdr!D.H.Davis Bath, BA2 7AY EARN/BITNET: D.H.Davis%uk.ac.bath@UKACRL England INTERNET: D.H.Davis%bath.ac.uk@nsfnet-relay.ac.uk