OT: Need some shell scripting hints ;)

Alexander Holst yellowdog-general@lists.terrasoftsolutions.com
Thu Oct 2 03:03:01 2003


Am Donnerstag, 02.10.03, um 09:39 Uhr (Europe/Berlin) schrieb 
yellowdog-general-request@lists.terrasoftsolutions.com:

> Message: 7
> Date: Wed, 1 Oct 2003 17:28:50 -0400 (EDT)
> From: "nathan r. hruby" <nhruby@uga.edu>
> To: yellowdog-general@lists.terrasoftsolutions.com
> Subject: Re: OT: Need some shell scripting hints ;)
> Reply-To: yellowdog-general@lists.terrasoftsolutions.com
>
> On Wed, 1 Oct 2003, Alexander Holst wrote:
>
>> for LINE in (every line of TextFile.txt)
>> do
>> 	some stuff with $LINE  # for every line one iteration
>> done
>>
>
> #!/bin/bash
> line=`cat file`
> for i in $line; do
>   /bin/something --to $i
> done
>
> Would do it.
> -n

> Message: 9
> Subject: Re: OT: Need some shell scripting hints ;)
> From: Konstantin Riabitsev <icon@phy.duke.edu>
> To: yellowdog-general@lists.terrasoftsolutions.com
> Organization: Duke University Physics
> Date: 01 Oct 2003 17:33:25 -0400
> Reply-To: yellowdog-general@lists.terrasoftsolutions.com
>
> On Wed, 2003-10-01 at 17:28, nathan r. hruby wrote:
>> #!/bin/bash
>> line=3D`cat file`
>> for i in $line; do
>>   /bin/something --to $i
>> done
>> =20
>> Would do it.
>
> Only if there are no whitespaces in the lines.

> Message: 10
> Date: Wed, 1 Oct 2003 17:44:56 -0400 (EDT)
> From: Jason DeVita <jdevita@umich.edu>
> To: yellowdog-general@lists.terrasoftsolutions.com
> Subject: Re: OT: Need some shell scripting hints ;)
> Reply-To: yellowdog-general@lists.terrasoftsolutions.com
>
> On Wed, 1 Oct 2003, Konstantin Riabitsev wrote:
>
>> On Wed, 2003-10-01 at 17:28, nathan r. hruby wrote:
>>> #!/bin/bash
>>> line=`cat file`
>>> for i in $line; do
>>>   /bin/something --to $i
>>> done
>>>
>>> Would do it.
>>
>> Only if there are no whitespaces in the lines.
>
> This is not a very elegant way to do it, but it should work:
>
> for x in $(awk '{print NR}' file)
> do
>     y=$(awk '{if(NR==x)print}' x=$x file)
>     do something to $y
> done

> Message: 11
> Date: Wed, 1 Oct 2003 16:47:37 -0500
> From: Jason Gilbert <jason@doozer.com>
> To: yellowdog-general@lists.terrasoftsolutions.com
> Subject: Re: OT: Need some shell scripting hints ;)
> Reply-To: yellowdog-general@lists.terrasoftsolutions.com
>
> On Wed, Oct 01, 2003 at 05:28:50PM -0400, nathan r. hruby wrote:
>> On Wed, 1 Oct 2003, Alexander Holst wrote:
>> =20
>>> for LINE in (every line of TextFile.txt)
>>> do
>>> 	some stuff with $LINE  # for every line one iteration
>>> done
>>> =20
>> =20
>> #!/bin/bash
>> line=3D`cat file`
>> for i in $line; do
>>   /bin/something --to $i
>> done
>
> I am not a shell expert by any stretch of the imagination.
>
> I found another alternative when trying to do this.
>
>    while read line
>    do
>       something with $line
>    done < "$some_file_name"
>
> My understanding is that it basically hooks up the file contents as 
> standar=
> d in
> to the read call.  This actually splits the line using the IFS 
> characters as
> delimiters (as would happen, I think, in the above example as well) 
> and puts
> them into as many variables as are supplied.  I just didn't have any 
> spaces,
> etc in my input.  However, look at the man page it appears that if you 
> just
> don't supply any variable names after 'read' then the line will be 
> present =
> in
> $REPLY.  I haven't tried this though.
>
> jason

> Message: 13
> Subject: Re: OT: Need some shell scripting hints ;)
> To: yellowdog-general@lists.terrasoftsolutions.com
> Date: Thu, 2 Oct 2003 10:37:36 +0300 (EEST)
> From: Atro Tossavainen <atro.tossavainen+ydl@helsinki.fi>
> Reply-To: yellowdog-general@lists.terrasoftsolutions.com
>
>> I'm stuck with writing a shell script that is supposed to read a text
>> file line by line.
>
> Use the "read" command in the shell.
>
> #!/bin/sh
>
> while read x
> do
> 	some stuff with $x
> done
>
> Run this as "script < file"
>
> Using "read" gets rid of the annoying problem in Nathan's suggestion
> that Konstantin pointed out that a "for" loop over `cat $file` would
> not loop over entire lines, but instead over whitespace-separated
> words in those lines too.

Thanks everybody for your comments on this!

I did experience the "whitespace problem" with Nathan's solution, which 
was my first attempt too, but did not come up with Jason's and Atro's 
solution ;) I fumbled around with:

while [ read <TextFile.txt LINE ]
do
	something with $LINE
done

which only gave me the first line of the file and looping forever :( It 
never appeared to me that the input from file must be at the end of the 
loop ;) - too much coffee & cigarettes, maybe I should stop smoking and 
pickup the habit of drinking tea =8]

Thanks again,
Alex


Alexander Holst
Pforzheim University of Applied Sciences
<holst@fh-pforzheim.de>
ph: +49 [0]7231 28-6837
fx: +49 [0]7231 28-6040