Read textfile and enter the values in array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read textfile and enter the values in array
# 1  
Old 02-25-2009
Read textfile and enter the values in array

Hi,

I want to put values in .txt file into array. Example :

$vi repo.txt
abc
def
ghi
jkl
mno
pqr


i want the output to be like this:
$echo ${mydf[1]}
abc
$echo ${mydf[2]}
def
$echo ${mydf[3]}
ghi
$echo ${mydf[4]}
jkl
$echo ${mydf[5]}
mno
$echo ${mydf[6]}
pqr

my script:
for i in 1 2 3 4 5 6
do
read mydf[$i] < repo.txt
print "mydf[$i] value is ${mydf[i]}"
done

What's wrong with my script? cuz anytime I echo ${mydf[1]}, I got empty output.. not abc...

Please helppp Smilie

Thanks Smilie

# 2  
Old 02-25-2009
Code:
for i in 1 2 3 4 5 6
do
  read mydf[$i]
  print "mydf[$i] value is ${mydf[i]}"
done < repo.txt

Or:

Code:
mydf=( $( cat repo.txt ) )
printf "%s\n" "${mydf[@]}"

# 3  
Old 02-25-2009
Hi cfajohnson,

I do not have problem on print "mydf[$i] value is ${mydf[i]}" as I get the correct output but I have problem when trying to echo the value of ${mydf[1]} on the command prompt after running this script. Any tips?
# 4  
Old 02-25-2009

A script cannot affect the environment of the calling shell unless it is sourced:

Code:
. script-name

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

How to separate sorte different characters from one textfile and copy them in a new textfile?

My first post, so don't kill me :) Say i open some textfile with some example like this. on the table are handy, bread and wine Now i know exactly what is in and i want to separate and sorted it in terminal to an existing file with another 2 existing lines in like this: table plane ... (3 Replies)
Discussion started by: schwatter
3 Replies

3. Shell Programming and Scripting

How to read values and store in array?

I am reading a value from a file and want to store the value in a dynamic array as i don't know the number of occurrences of the value in that file. How can i do that and then later fetch that value from array (25 Replies)
Discussion started by: Prachi Gupta
25 Replies

4. Shell Programming and Scripting

how to prompt the user to enter an array in tcsh

Hello, I am writing a script that requires the user to enter a string of numbers: ex: 134 345 865 903 This command only allows for one variable to be entered: set "var" = $< and than once I got the array I want to change it to a list with each input on a different line: ... (1 Reply)
Discussion started by: smarones
1 Replies

5. Shell Programming and Scripting

awk search strings from array in textfile

I am wanting to take a list of strings and loop through a list of textfiles to find matches. Preferably with awk and parsing the search strings into an array. // Search_strings.txt tag string dummy stuff things // List of files to search in textfile1.txt textfile2.txt The... (6 Replies)
Discussion started by: sdf
6 Replies

6. Shell Programming and Scripting

Read variables names from array and assign the values

Hi, I have requirement to assign values to variables which are created dynamically. Below is the code which i am using to achieve above requirement. #!/bin/ksh oIFS="$IFS"; IFS=',' STR_FAIL_PARENT_IF_FAILS="WF_F_P_IF_FAILS1,WF_F_P_IF_FAILS2,WF_F_P_IF_FAILS3" set -A... (1 Reply)
Discussion started by: tmalik79
1 Replies

7. Shell Programming and Scripting

using read to enter the input at runtime

Hi I am stucked in the below script .I want to input with yes/no from the user and then execute the code inside if but it is not working .I just need the logic as where I am wrong so that i can use the same in my work . then echo "Hi All" fi ]. Please suugest . (4 Replies)
Discussion started by: mani_isha
4 Replies

8. Homework & Coursework Questions

Extracting and Coverting values from a K12 Textfile (Wireshark capture)

1. The problem statement, all variables and given/known data: Using the Windows version of Wireshark, record at least 100 entries of normal network traffic (in a file). Pick 2 packets and analyze the contents showing the header and data information (ie what port #, service etc). You can use the... (0 Replies)
Discussion started by: KKWL
0 Replies

9. Shell Programming and Scripting

Not able to read unique values in array

Hi Friends, I am having some trouble reading into an array. Basically, I am trying to grep for a pattern and extract it's value and store the same into an array. For eg., if my input is: <L:RECORD>name=faisel farooq,age=21, company=TCS,project=BT</L:RECORD> <L:RECORD>name=abc... (1 Reply)
Discussion started by: faiz1985
1 Replies

10. Shell Programming and Scripting

read the ENTER key

Hi , I do the following : ]echo "Do you want to say yes or no ?(y/n):\c" read ans here 'n' is the default value.that means if the user press ENTER key then it should be 'n' . Now how do i know that the user has pressed ENTER key.What will be stored in my variable 'ans'. (4 Replies)
Discussion started by: sars
4 Replies
Login or Register to Ask a Question