[ale] OT: Bash Question
Geoffrey
esoteric at 3times25.net
Wed Apr 3 12:46:00 EST 2002
I'm a ksh'er by nature, but threw this together. I'm a stickler for
saving processes, read that as fewer pipes. Not a big deal these days,
but it was on the 3b2 with 8 meg of memory.... :)
#!/bin/bash
# input file name is 'foo'
half_rec=$(($(wc -l < foo) / 2))
c=0
while read data && [ $((c=c+1)) -le $half_rec ]; do
echo $c $data
done < foo
Asif wrote:
> The following works for me ( both for even,odd and 0-numbered list in the list
> file )
>
> #!/bin/bash
>
> file=$1
> count=`wc -l $file | awk '{ printf "%i", $1/2 }'`
> i=0;
> for item in `cat $file`
> do
> if [ $i -lt $count ]
> then
> i=`expr $i + 1`
> echo $i $item
> fi
> done
>
>
> Hope it helps.
>
> Asif
>
> Jeff Layton wrote:
>
>
>>Sorry about the OT, but I didn't know where else to turn.
>>
>> I'm writing a simple shell script that cats a file to a
>>variable (the file is just a list). I want to loop over only
>>HALF of that list. So I want something like this:
>>
>>NODES=$(cat node.txt)
>>
>>for i in ${NODES}/2
>>do
>> echo ${i}
>>done
>>
>>Of course, this doesn't work.
>> I've also tried "counting" the number of items in the
>>list, dividing by 2, and using a while loop. The only thing
>>is, I don't know how to access a particular element in
>>the list,
>>
>>NODE_FILE="node.txt"
>>NODES=$(cat node.txt)
>>
>>declare -i NP
>>declare -i NP2
>>NP=`wc -l $NODES | awk '{print $1}'`
>>NP2=NP/2
>>
>>iloop=1
>>while (( $iloop <= ${NP2} ))
>>do
>> # Here I want to echo the "iloop" element of $NODES
>> let iloop+=1
>>done
>>
>> I really appreciate any help anyone can give. I'm also
>>pretty tolerant of comments about my scripting capabilities :)
>>
>>Thanks!
>>
>>Jeff Layton
>>
>>---
>>This message has been sent through the ALE general discussion list.
>>See http://www.ale.org/mailing-lists.shtml for more info. Problems should be
>>sent to listmaster at ale dot org.
>>
>
>
> ---
> This message has been sent through the ALE general discussion list.
> See http://www.ale.org/mailing-lists.shtml for more info. Problems should be
> sent to listmaster at ale dot org.
>
>
>
--
Until later: Geoffrey esoteric at 3times25.net
I didn't have to buy my radio from a specific company to listen
to FM, why doesn't that apply to the Internet (anymore...)?
---
This message has been sent through the ALE general discussion list.
See http://www.ale.org/mailing-lists.shtml for more info. Problems should be
sent to listmaster at ale dot org.
More information about the Ale
mailing list