Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linux - read file into an array

Status
Not open for further replies.
Apr 11, 2002
144
US
I am trying to create a basic script that needs to read the contents of a file into an array. I have read some of the posts here but nothing seems to work, I think because I'm using bash on RH8. I know for sure that "set -A" won't work in this version of bash.

The file to be read has one entry per line, nothing complex.

I understand how to access and manipulate the array elements once they are in there, but getting the contents of the file into it has been tricky.

Any ideas will be appreciated.


>Think for yourself<
...or someone else will do it for you.
 
This is how I did it:

declare -a array
cat filename | while read -a array

I was missing the &quot;-a&quot; in read. Works like a champ.

Now my problem is sort.
If I do this:

sort +6n -t&quot; &quot; filename

it will sort the 7th field numerically, but the results are like this:
107
111
1313
239
240
242
253 etc;, etc;. In other words, the number 2 would be sorted before the number 10, because the first digit is a higher number.


>Think for yourself<
...or someone else will do it for you.
 
Thanks.

Yea, the O'Reilly &quot;Linux in a nutshell&quot; says you can put the &quot;n&quot; for numeric right after the &quot;+2&quot;. That doesn't work. The &quot;-n&quot; must be all by itself.


>Think for yourself<
...or someone else will do it for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top