Can I assign the contents of file into an array?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can I assign the contents of file into an array?
# 8  
Old 03-27-2007
Do you have korn shell ksh or you are running into some other shell.
pls switch it to korn shell to use arrays.
Code:
#!/usr/bin/ksh
x=`ls skm`
echo $x
set -A look $x
echo ${look[0]}

# 9  
Old 03-27-2007
Quote:
Originally Posted by murtaza
Thank u for reply. Can u tell me the concept about the set * command?
Positional parameters

Quote:
A positional parameter is a parameter denoted by one or more digits, other than the single digit 0. Positional parameters are assigned from the shell's arguments when it is invoked, and may be reassigned using the set builtin command. Positional parameter N may be referenced as ${N}.
With trace enabled (the -x option) you can see how it works:

Code:
$ set -x
$ set *
+ set a b c

Or, if you insist to use "ls" (with bash, for ksh93 use typeset -A):

Code:
$ a=($(ls))
$ echo ${a[0]}
a
$ echo ${a[1]}
b
$ echo ${a[@]}
a b c
$ echo ${#a[@]}
3

# 10  
Old 03-27-2007
sorry i have only bash. and ur given command are not working on bash and giving same error. Thanks
Please tell me the solution for the bash if u have. Thanks for reply again in advance
# 11  
Old 03-28-2007
have you ever tried man pages Smilie

Code:
billym.>A=($HOME/*)
billym.>for x in 1 2 3 4 5;do echo ${A[$x]};done
/export/home/billym/1.c
/export/home/billym/1.cpp
/export/home/billym/1.pl
/export/home/billym/1.sh
/export/home/billym/1.xml

Login or Register to Ask a Question

Previous Thread | Next Thread

10 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. Shell Programming and Scripting

Place the contents of a .CSV file to an array

Hi, I am trying to place the contents of a .CSV file to an array, but not sure how to do that. Here is my .CSV file content: App,SLA,Job name,Avg start time,Avg run time,Frequency,Downstream apps XYZ,,ABC14345,3:00 AM,00.04.00,Daily,STAMP XYZ,9:00,ABC12345,3:15 AM,00.05.00,Daily,STAMP ... (4 Replies)
Discussion started by: ajayakunuri
4 Replies

3. Shell Programming and Scripting

How can we assign value to an array variable from an external file?

is it possible to assign value to an array variable from an external file?? if yes then how?? I am using below code but its not working. #!bin/bash myarray < file_name echo ${mayarray} (6 Replies)
Discussion started by: mukulverma2408
6 Replies

4. Shell Programming and Scripting

how to assign file names to array variable?

I wish to assign file names with particular extention to array variables. For example if there are 5 files with .dat extention in /home/sam then i have to assign these 5 files to an array. plz help me how to accomplish this. Thanks in advance. (4 Replies)
Discussion started by: siteregsam
4 Replies

5. Shell Programming and Scripting

Write array contents to file

Hi, I have a bash script that currently holds some data. I am trying to write all the contents to a file called temp.txt. I am using echo ${array} > temp.txt The problem that I am experiencing is that the elements are being written horizontally in the file. I want them written... (5 Replies)
Discussion started by: Filter500
5 Replies

6. 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

7. Shell Programming and Scripting

Most reliable way to store file contents in an array in bash

Hi Guys, I have a file which has numbers in it separated by newlines as follows: 1.113 1.456 0.556 0.021 -0.541 -0.444 I am using the following code to store these in an array in bash: FILE14=data.txt ARRAY14=(`awk '{print}' $FILE14`) (6 Replies)
Discussion started by: npatwardhan
6 Replies

8. Shell Programming and Scripting

read contents of a file with serveral lines & assign it to a variable

Hi All I have a file for ex .log file which contain several lines within it. I have to read that file contents & assing that to a variable. (2 Replies)
Discussion started by: satyam.sumit
2 Replies

9. Shell Programming and Scripting

PERL - copy fiel contents to array then compare against other file

Basically to illuminate i want to take a file with mutliple lines, C:\searching4theseletters.txt a b c Read this into an array @ARRAY and then use this to compare against another file C:\inputletters.txt b o a c n a (9 Replies)
Discussion started by: bradleykins
9 Replies

10. UNIX for Dummies Questions & Answers

compare array contents with file

I have an array "arrA" with the following contents: A0012 Paint Shop.doc ES001 Contract Signature.doc Budget Plan.pdf TS PWS.pdf My data file "Files.dat" has the same contents: A0012 Paint Shop.doc ES001 Contract Signature.doc Budget Plan.pdf TS PWS.pdf I have a script that compares... (0 Replies)
Discussion started by: orahi001
0 Replies
Login or Register to Ask a Question