*BSD News Article 74700


Return to BSD News archive

Newsgroups: comp.unix.bsd.freebsd.misc
Path: euryale.cc.adfa.oz.au!newshost.anu.edu.au!harbinger.cc.monash.edu.au!munnari.OZ.AU!news.ecn.uoknor.edu!news.wildstar.net!news.sdsmt.edu!news.mid.net!mr.net!news.sgi.com!news.msfc.nasa.gov!newsfeed.internetmci.com!newsreader.sprintlink.net!news.sprintlink.net!news-pen-4.sprintlink.net!news.sprintlink.net!news-dc-10.sprintlink.net!rockyd!dnn.rockefeller.edu!dan
From: dan@dnn.rockefeller.edu (Dan Ts'o)
Subject: Re: sh command shell script writing. i'm stuck
X-Nntp-Posting-Host: dnn.rockefeller.edu
Message-ID: <Dv4rGs.2nv@rockyd.rockefeller.edu>
Sender: notes@rockyd.rockefeller.edu (News Administrator)
Organization: Rockefeller University
X-Newsreader: TIN [version 1.2 PL2]
References: <4t02d1$nec@cliff.island.net> <Pine.HPP.3.91.960722171415.29481F-100000@lint.cisco.com>
Date: Fri, 26 Jul 1996 03:10:03 GMT
Lines: 47

Sridhar Krishnan (skrishna@cisco.com) wrote:
: If you have (n)awk, try something like:

: awk -v x=8 '(NR==x){ print $0; exit }'  file

: x = line number you wish to print.

: On Mon, 22 Jul 1996, Victor Aker wrote:

: > I am trying to import text one line at a time into a shell script that I am 
: > writing, and having a real bugger of a time. I need to grab these lines one by 
: > one, and explicitly state each time which line number it is (Only grabbing, as 
: > an example, line 3 and line 3 only). So far, I have gotten this far:
: > 
: > tempuser=$(ls /home | cat -n | grep -G "$counter"
: > 
: > where counter is the number of the line. One problem, this output is giving me 
: > the following:
: > 
: >         3       ets
: > 
: > where I only need
: > 
: > ets

	Others have given you solutions as to how to read in one line at a
time into a script.  But these are all a little nasty (because they run awk
or grep, which is pretty big and then that program re-reads the whole file
just to pick out another line). Perhaps you could expand on just what you
are trying to do. There maybe a more efficient way.
For example, how about something like:

(
	while read line
	do	
		# Do whatever
		echo "Here is another line: $line"
	done
) < input_file

--
			Cheers,
			Dan Ts'o			212-327-7671
                        Dept. of Neurobiology   	FAX: 212-327-7671
                        The Rockefeller University
                        1230 York Ave.  Box 138		dantso@cris.com
                        New York, NY  10021     	dan@dna.rockefeller.edu