Read array from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read array from a file
# 1  
Old 09-28-2009
Read array from a file

Hi

I've a config file like:

file1
Code:
#comment

k_array: 1 2 3 4 5
n_array: 7 8 9 0 11

I'd like to write a script that read it and store k_array and n_array in 2 arrays.
I mean the script should be able to use both as array.

I've tried to use awk as (only for one array):
Code:
k_array2=$(awk '{ for (x = 1; x <= NF; x++) 

arr[x,NR]=$x; 
}
{ if (arr[1,NR] == "k_array:") {
    for(j=2;j<=NF;j++)
        k_array[j-1]=$j;
            
    }
} END{ for(j=1;j<=length(k_array);j++) print k_array[j]}' config_file)

but in this way the returned k_array seems to be a single element array.
In fact with

Code:
echo ${k_array2[0]}

I get ------> 1 2 3 4 5.

Any help?

Thanks

D.
# 2  
Old 09-28-2009
One way to do it with Perl:

Code:
$
$ cat file1
#comment
 
k_array: 1 2 3 4 5
n_array: 7 8 9 0 11
$
$ perl -lne 'BEGIN{$x=0} chomp; if(/.*: (.*)/){if ($x==0){@a1 = split/ /,$1;$x=1}
>            else{@a2 = split/ /,$1}} END {print "\@a1 = @a1\n\@a2 = @a2"}' file1
@a1 = 1 2 3 4 5
@a2 = 7 8 9 0 11
$
$

Otherwise -

Code:
$
$ cat file1
#comment

k_array: 1 2 3 4 5
n_array: 7 8 9 0 11
$
$
$ ##
$ awk 'BEGIN {x=0} {
>       if (/:/ && x==0) { sub(/^.*: /,"",$0); split($0,a1); x=1}
>       else if (/:/ && x==1) { sub(/^.*: /,"",$0); split($0,a2)}
>      } END {
>          for (i=1; i<=length(a1); i++) {print "a1["i"] = "a1[i]}
>          for (i=1; i<=length(a2); i++) {print "a2["i"] = "a2[i]}
>      }' file1
a1[1] = 1
a1[2] = 2
a1[3] = 3
a1[4] = 4
a1[5] = 5
a2[1] = 7
a2[2] = 8
a2[3] = 9
a2[4] = 0
a2[5] = 11
$
$

tyler_durden

Last edited by durden_tyler; 09-28-2009 at 12:15 PM..
# 3  
Old 09-28-2009
Quote:
Originally Posted by Dedalus
Hi

I've a config file like:
[...]
What shell you're using?
# 4  
Old 09-28-2009

In bash or (I think) ksh93:

Code:
set -f  ## not necessary with example given.
while IFS= read -r line
do
  case $line in 
    *array:*) name=${line%%:*}
              eval "$name=( ${line#*:} )"
              ;;
  esac
done < "$file"
set +f

# 5  
Old 09-28-2009
hi

i use /bin/bash

@durden_tyler: thx for the code.

@cfajohnson: thx for the code too

I'm checking out that someone to put the result in one array use:

Code:
set -A k_array2 `awk '{ code awk that return array} '`

is it correct? becouse i get an error as invalid option for set

thanks

D

Last edited by Dedalus; 09-28-2009 at 12:26 PM..
# 6  
Old 09-28-2009
Using Bash, I get almost the same as cfajohnson
Code:
while read line 
 do
   [[ $line =~ ^._array ]] && eval "${line%:*}=( ${line#*:} )"
done < configFile1

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How To Read a File and Assign the line values to an Array?

i have this basic code that i wrote to read a file and place it's values to an array. the source/input file will have multiple strings on it that is separated by a whitespace. sample_list.txt file contents: ACCT1 TABLE1 ACCT2 TABLE2 ACCT3 TABLE3 script file: sample_list.sh ... (3 Replies)
Discussion started by: wtolentino
3 Replies

2. Programming

C++ how would you read this file into an array.

here is the pesudo file. REREREEEEEERRRREER SOMEStrinG1234 RERRRR EEERRRREER SOMEStrinG1224 REREREEEREERRR REE SOMEStrinG1214 REREREREREREREEEER SOMEStrinG1204 RERE EEEEEERRRRRRR SOMEStrinG1294 REREEREEE ERRRREER SOMEStrinG1284 REREREEEEEERR REER here is my attempted code #include... (3 Replies)
Discussion started by: briandanielz
3 Replies

3. Shell Programming and Scripting

Read file and get the data then put it in array

Hi, I have a file called "readfile" it contains below parameters #cat readfile word=/abc=225,/abc/cba=150 three=12 four=45 five=/xyz/yza likewise multiple line. From the above file, I have to read "word" output should be like, /abc /abc/cba these values need to be put in... (3 Replies)
Discussion started by: munna_dude
3 Replies

4. UNIX and Linux Applications

Perl Script to read an excel file into an array and search in the UNIX directories

Hi, I want the Perl script with versions 5.8.2 and 5.8.5 starting with #!/usr/bin/perl The Perl program should read the excel file or text file line by line and taking into an array and search in the UNIX directories for reference file of .jsp or .js or .xsl with path .The Object names... (2 Replies)
Discussion started by: pasam
2 Replies

5. Shell Programming and Scripting

perl: Read array from a flat file

Hello Guru's I want to read an array into a flatfile Please let me know how to do the same So far this the below code use strict; use warnings; open (my $data , '<', $ARGV)|| die "could not open $ARGV:\n$!"; my @array=(<$data>); my @sorted=sort... (8 Replies)
Discussion started by: Pratik4891
8 Replies

6. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

7. Shell Programming and Scripting

Issues using array credentials to read contents of a file

Hi, I am trying to read the contents of a file using array credentials in unix. The file I am trying to read is tab separated and contains the below contents. # partnerid Direc Server Port Source_Dir Target_Dir Mask Remove Files Passwordless Compare Files ... (3 Replies)
Discussion started by: aartikara
3 Replies

8. Shell Programming and Scripting

how to read a var value into array

Hi I need to read a value of the variable into array so each character/digit will become an array element,for example: A=147921231432545436547568678679870 The resulting array should hold each digit as an element. Thanks a lot for any help -A (7 Replies)
Discussion started by: aoussenko
7 Replies

9. UNIX for Dummies Questions & Answers

How to read from txt file and use that as an array

Hi Guys How u all doing? I am having tough time to achieve this I have a unix .ksh script which calls sql script Right now I harcoded column id's in sql script but I want to read them from a txt file 1084,1143,1074,1080,1091,1090,1101,1069,1104,1087,1089,1081 I want to read this... (4 Replies)
Discussion started by: pinky
4 Replies
Login or Register to Ask a Question