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?
# 1  
Old 03-27-2007
Can I assign the contents of file into an array?

I am trying to assign the contents of file e.g

ls "$HOME"

into an array. If it is possible then please guide me without using the concept of awk,sed, and perl16

Thanks
# 2  
Old 03-27-2007
Code:
set -A arr `ls"$HOME"`

# 3  
Old 03-27-2007
Thanks for reply but this command is giving an error.
# 4  
Old 03-27-2007
pls paste here the error message and the task you want to do.what you want to do either assign contents of file to an array or the result of ls command to array.
# 5  
Old 03-27-2007
I just want the result for ls command to array. Thanks for interest

ds: line 4: set: -A: invalid option
set: usage: set [--abefhkmnptuvxBCHP] [-o option] [arg ...]
# 6  
Old 03-27-2007
Quote:
Originally Posted by murtaza
I am trying to assign the contents of file e.g

ls "$HOME"

into an array.
[...]

You can do it without "ls":

Code:
$ ls
a  b  c
$ set *
$ echo ${1} # first element
a
$ echo ${2} # second
b
$ echo ${@} # all
a b c
$ echo ${#@} # number of elements
3
$ eval echo \${${#@}} # last element
c

# 7  
Old 03-27-2007
Thank u for reply. Can u tell me the concept about the set * command?
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