Re: reading lines from standard input or a two way pipe into an array



On 26 Nov., 10:26, hyperboogie <hyperboo...@xxxxxxxxx> wrote:
Hi

I'm trying to read the output of 'ls -lartF' into an array, a line
into each array element
I've tried to do this in 3 different ways

1. I've managed to do this is using a two way pipe, as follows:

#!/bin/ksh

i=0
ls -lartF |&
while read -p line; do
print - "$line"
aline[i]="$line"
let i=i+1
done

Is there a way of doing the this without being a smartass (i.e.
without two way pipes)

2. In the following way it's all read as a single line into the first
array element:

#!/bin/ksh

i=0
print -r $(ls -lartF) > tmpshiron
while read -r line; do
print -r "$line"
aline[i]="$line"
let i=i+1
done < tmpfile
rm tmpfile

how can I read the output as lines?
is there a way I can read the lines without using a tempfile ???

3. I've tried (and failed) "feeding" the loop from the subprocess, as
follows:

while read -r line; do
..
..
done < $(ls -lartF)

what is the most straight forward way of doing this?

Simple pipes...

ls -lartF |
while read -r line; do
...
done

Or, if you want to work on separate fields within the loop, enumerate
the fields in the read list...

ls -lartF |
while read -r var1 var2 var3 rest_of_line
do
...
done


Janis


Thanks
Shiron

.



Relevant Pages

  • Re: Common Lisp implementations are still multiple times slower than C
    ... ARRAY element type when doing MAKE-ARRAY. ... that a simple-vector can hold every element. ... used "(loop for y fixnum from 0 below height..." ...
    (comp.lang.lisp)
  • Re: Efficient solution needed
    ... > Can any one plzz solve the following problem in an efficient way... ... > Multiplies this number by 2. ... A simple loop, ... get a number for which the array element is already 1. ...
    (comp.programming)
  • Re: array question
    ... "RB Smissaert" wrote: ... a differing array element early in the loops so exit early. ... The only refinement I can think of is loop in such a way that you are ...
    (microsoft.public.excel.programming)
  • Re: how to search d15?
    ... of each array element? ... but I'm too busy to write a test example right now. ... concatenating the results into the label text in a loop. ... But not getting "d15" AT ALL? ...
    (microsoft.public.vb.general.discussion)
  • Re: Removing entry from @rray
    ... Redefining the @array element inside to loop ... WORD must be delimited by word boundaries, ... match is found I would want to terminate the loop so that it doesn't waste time checking all the other array elements for a match. ...
    (comp.lang.perl.misc)