Put file lines in an array: how keep line blanks in the string?

Put file lines in an array: how keep line blanks in the string?

Post by mon.. » Tue, 07 Nov 2000 04:00:00



Hi,
The pb is that if file 'myFile.txt' contains:

Quote:> here's line 1
> here's line 2
> and eventually the 3rd line

If I do "set -A fileArray `cat myFile.txt`", I'll get:
Quote:> here's
> line
> eventually
> the
> 3rd

with the following commands:

Quote:> echo ${fileArray[0]}
> echo ${fileArray[1]}
> echo ${fileArray[7]}
> echo ${fileArray[8]}
> echo ${fileArray[9]}

But of course I'd like to retrieve EACH line for a single
call to the "echo" command corresponding to a given line
number (minus one)...

Do you know how I could store each string in each cell
of my array (for ex. "here's line 1" in $fileArray[0]) ?

Thanks a lot !

Regards,
Seb

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

Put file lines in an array: how keep line blanks in the string?

Post by laura fairhe » Tue, 07 Nov 2000 04:00:00



>Hi,
>The pb is that if file 'myFile.txt' contains:
>> here's line 1
>> here's line 2
>> and eventually the 3rd line

>If I do "set -A fileArray `cat myFile.txt`", I'll get:
>> here's
>> line
>> eventually
>> the
>> 3rd
>with the following commands:
>> echo ${fileArray[0]}
>> echo ${fileArray[1]}
>> echo ${fileArray[7]}
>> echo ${fileArray[8]}
>> echo ${fileArray[9]}

>But of course I'd like to retrieve EACH line for a single
>call to the "echo" command corresponding to a given line
>number (minus one)...

>Do you know how I could store each string in each cell
>of my array (for ex. "here's line 1" in $fileArray[0]) ?

Try this;
$ IFS="

Quote:> " set -A fileArray $(cat myFile.txt)

(After the IFS=" enter a newline, you get the 2nd prompt ">" and
continue)

I haven't completely finished reading up on all this stuff
but this intuitive try works for me. Notice it doesn't
work if you use the backquote method of command substitution
instead.

Bye,

L

- Show quoted text -

Quote:>Thanks a lot !

>Regards,
>Seb

>Sent via Deja.com http://www.deja.com/
>Before you buy.


 
 
 

Put file lines in an array: how keep line blanks in the string?

Post by kevin_coll.. » Tue, 07 Nov 2000 04:00:00


Try this:

typeset -i cnt=0
set -A FileArray

while read line
do
   FileArray[$cnt]="$line"
   ((cnt=$cnt+1))
done < myFile.txt

Kevin



> Hi,
> The pb is that if file 'myFile.txt' contains:
> > here's line 1
> > here's line 2
> > and eventually the 3rd line

> If I do "set -A fileArray `cat myFile.txt`", I'll get:
> > here's
> > line
> > eventually
> > the
> > 3rd
> with the following commands:
> > echo ${fileArray[0]}
> > echo ${fileArray[1]}
> > echo ${fileArray[7]}
> > echo ${fileArray[8]}
> > echo ${fileArray[9]}

> But of course I'd like to retrieve EACH line for a single
> call to the "echo" command corresponding to a given line
> number (minus one)...

> Do you know how I could store each string in each cell
> of my array (for ex. "here's line 1" in $fileArray[0]) ?

> Thanks a lot !

> Regards,
> Seb

> Sent via Deja.com http://www.deja.com/
> Before you buy.
> echo ${x##*/}

Sent via Deja.com http://www.deja.com/
Before you buy.
 
 
 

1. vi: convert double blank lines to single blank lines???

howabou
sed "/./,/^$/!d" file

or, more generally
sed "/./,/^ *$/!d" file

( copyright Brian Hiles, or is it Greg Uben ? )

                        ^^^^^^^^^^^^^^^^^^^^^^^^^^
                         JOIN SEDERS MAILING LIST
                            DISCUSS SED WITH US
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^

: >
: > I receive some files which contain, among other amusements, 2 (or more)
: > consecutive blank lines. I wish to compress every occurrence of multiple
: > blank lines to one blank line. I have already suppressed leading (and,
: > for that matter, trailing) white space, so all the lines actually match
: > /^$/ if that helps.
: >
: > Personally, I think it is not possible, but there are many of you out
: > there with even more perverse minds than mine. Thanks for looking at my
: > problem. Responses either here to the newsgroup, or, if you think this
: > too esoteric for general interest, to my E-mail address below.

: Not sure if this is supposed be done in vi. I think it should, and Sven
: has shown how to do this. Otherwise, you could use:

:   :%!cat -r

: Purely for esoteric interest :-> I include this sed script:

:   #!/usr/bin/sed -f
:   # on empty lines, join with next
:   :x
:   /^\n*$/{
:   N
:   bx
:   }
:   # now, squeeze all newlines
:   s/^\(\n\)*/\1/

: Casper Boden-Cummins.
--
=-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
al aab, seders moderator                                      sed u soon
               it is not zat we do not see the  s o l u t i o n          
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+

2. RAM interleaving with performance?

3. multiple blank lines -> one blank line

4. Useradministration on Apache/PHP/MySQL

5. vi: convert double blank lines to single blank lines???

6. ipchains -- support for DHCP?

7. Find a string, delete that line, delete 1 line before it and all lines after it...

8. GCC 2.7.2 (internal err 11), help...

9. adding new line to external text file below a line that contains <string>

10. Vi -- How to Insert a Blank Line to all existing lines

11. inserting a blank line after every line - using sed

12. line number of the first blank line?

13. Remove a line if next line is blank