[net.sources.bugs] YASS

raymund@sci.UUCP (Raymund Galvin) (03/15/86)

In article <620@ssc-vax.UUCP>, aims@ssc-vax.UUCP (John Daley) writes:
> This is a 'simple' little shell script I whipped up, out of need, to
> print a specific line of a specific file.  Yes, I chose to use the B
> shell, but it gets the job done, right?.
> 
> Why am I posting such a meager little script?  This newsgroup hasn't
> been very active lately (excluding discussions).  Anybody out there
> got a better way to do what my script does?
        ^^^^^^

Better?  I dont know.  I dont have access to B shell. 

The following alias will print any single line from a text file:
	alias line 'awk NR==\!:1 \!:2-$'
Example: "line 10 .login" would print  the tenth line from .login


The following alias will print a range of lines from a text file:
	alias list 'awk NR==\!:1,NR==\!:2\{print\ NR\":\"\$0\} \!:3-$'
Example: "list 9 11 .login"  would print lines  9 - 11  from .login 

avolio@decuac.UUCP (Frederick M. Avolio) (03/18/86)

In article <153@sci.UUCP>, raymund@sci.UUCP (Raymund Galvin) writes:
> In article <620@ssc-vax.UUCP>, aims@ssc-vax.UUCP (John Daley) writes:
> > This is a 'simple' little shell script I whipped up, out of need, to
> > print a specific line of a specific file. 
> 
> Better?  I dont know.  I dont have access to B shell. 
> 
> The following alias will print any single line from a text file:
> 	alias line 'awk NR==\!:1 \!:2-$'
> The following alias will print a range of lines from a text file:
> 	alias list 'awk NR==\!:1,NR==\!:2\{print\ NR\":\"\$0\} \!:3-$'

The problem with the awk versions is that they process the whole file,
even after you are no longer interested in output. So, 

	line 10 /etc/termcap

does take a while after printing line #10.  

John's script does use the Bourne Shell, but his basic algorithm (head
piped to tail) is usable as an alias, and is quicker than using awk
for the above-mentioned reasons.  (5 times faster to *return* on
/etc/termcap ... rough timing.)
-- 
Fred @ DEC Ultrix Applications Center
UUCP: {decvax,seismo,cbosgd}!decuac!avolio       INET: avolio@decuac.DEC.COM

chris@umcp-cs.UUCP (Chris Torek) (03/18/86)

In article <860@decuac.UUCP> avolio@decuac.UUCP (Frederick M. Avolio)
writes:

>In article <153@sci.UUCP>, raymund@sci.UUCP (Raymund Galvin) writes:
>> The following alias will print any single line from a text file:
>>	alias line 'awk NR==\!:1 \!:2-$'
>> The following alias will print a range of lines from a text file:
>>	alias list 'awk NR==\!:1,NR==\!:2\{print\ NR\":\"\$0\} \!:3-$'
>The problem with the awk versions is that they process the whole file,
>even after you are no longer interested in output.

Not if you do it right:

	alias line "awk 'NR == \!:1 { print; exit }' \!:2*"
	alias list "awk 'NR == \!:1, NR == \!:2 { print NR "'":" $0 } NR == \!:2+1 { exit }'"' \!:3*"
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1415)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@mimsy.umd.edu

msb@lsuc.UUCP (Mark Brader) (03/20/86)

Frederick M. Avolio:
> >The problem with the awk versions is that they process the whole file,
> >even after you are no longer interested in output.
Chris Torek:
> Not if you do it right:
> 	alias line "awk 'NR == \!:1 { print; exit }' \!:2*"

You people should read net.sources.d.  Jan Gray posted this there days ago:
	sed -n -e $1p -e $1q $2

Not only is it faster than awk, it's short enough that you can just type it.
Who needs an alias or script for a line that short?

Oh, and besides THAT, it's more portable than the original.
More people have sed than head.

Mark Brader
P.S. Most lines of the form:	grep this | sed "s/that/the other/"
     can also be easily turned into single sed -n command.