*BSD News Article 74501


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.anu.edu.au!harbinger.cc.monash.edu.au!news.mira.net.au!inquo!news.uoregon.edu!news-res.gsl.net!news.gsl.net!news.mathworks.com!fu-berlin.de!irz401!orion.sax.de!uriah.heep!news
From: j@uriah.heep.sax.de (J Wunsch)
Newsgroups: comp.unix.bsd.freebsd.misc
Subject: Re: sh command shell script writing. i'm stuck
Date: 22 Jul 1996 22:52:19 GMT
Organization: Private BSD site, Dresden
Lines: 42
Message-ID: <4t10n3$7bn@uriah.heep.sax.de>
References: <4t02d1$nec@cliff.island.net>
Reply-To: joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch)
NNTP-Posting-Host: localhost.heep.sax.de
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Newsreader: knews 0.9.6
X-Phone: +49-351-2012 669
X-PGP-Fingerprint: DC 47 E6 E4 FF A6 E9 8F  93 21 E0 7D F9 12 D6 4E

bryanm@north.island.net (Victor Aker) wrote:

>  (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

First, your grep can get false hits, e.g. if some of the /home dirs
has a number in its name.  Combined with your other problem, i think
it's best to feed all this into awk:

	tempuser=$(ls /home | awk 'NR == '$counter)

Since there are no line numbers in front of the directory names now,
and the implied action for awk is to print the entire input line on
match, all you need is a pattern.

Since you only need one line, you can also shortcut it after this line
has been found, and exit immediately:

	tempuser=$(ls /home | awk 'NR == '$counter' {print; exit}')

Please, refer to the manual page for awk, or to the info files (``info
gawk'') for more information on the magic above.

Btw., you might also find Perl nice for this kind of work.

-- 
cheers, J"org

joerg_wunsch@uriah.heep.sax.de -- http://www.sax.de/~joerg/ -- NIC: JW11-RIPE
Never trust an operating system you don't have sources for. ;-)