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


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How To Read a File and Assign the line values to an Array?
# 1  
Old 11-05-2019
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:
Code:
ACCT1 TABLE1
ACCT2 TABLE2
ACCT3 TABLE3


script file: sample_list.sh
Code:
#!/bin/bash

vFileDir="sample_list.txt"

declare -a vLnArray

printf " \n"
#read the content of the file by line
while IFS= read vline
do
 vLnArray=("$vline")
 vStr1="${vLnArray[0]}"
 vStr2="${vLnArray[1]}"

 echo "vStr1: $vStr1"
 echo "vStr2: $vStr2"

 vLnCtr=$((vLnCtr+1))
done < "$vFileDir"


when i run the script it gives me this output:
Code:
vStr1: ACCT1 TABLE1
vStr2:
vStr1: ACCT2 TABLE2
vStr2:
vStr1: ACCT3 TABLE3
vStr2:


basically this is the expected output:
Code:
vStr1: ACCT1 
vStr2: TABLE1
vStr1: ACCT2 
vStr2: TABLE2
vStr1: ACCT3 
vStr2: TABLE3


please help thank you.
# 2  
Old 11-05-2019
Hi, try:
Code:
#!/bin/bash

vFileDir="sample_list.txt"

printf " \n"
#read the content of the file by line
while read vStr1 vStr2
do
 echo "vStr1: $vStr1"
 echo "vStr2: $vStr2"

 vLnCtr=$((vLnCtr+1))
done < "$vFileDir"

--
Note: in your sample code, if you still want to use arrays, in this case you would need to use:
Code:
vLnArray=($vline)

without the quotes, otherwise the shell will not split the variable vline into two separate fields.

Last edited by Scrutinizer; 11-05-2019 at 05:05 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 11-06-2019
thanks so much both solutions works.
# 4  
Old 11-06-2019
vLnArray=( $vline ) without quotes does word splitting (wanted) and glob matching (not wanted).
A safer method is to let the read do the word splitting, either into discrete variables (see post #2) or into an array as follows:
Code:
while read -a vLnArray
do
 vStr1="${vLnArray[0]}"
 vStr2="${vLnArray[1]}"


Last edited by MadeInGermany; 11-06-2019 at 02:55 PM.. Reason: typo
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Do While Loop + Read From File + assign line to a variable

Hello, I am using below code for reading from a file and assigning the values to a variable , but it is loosing the value after the loop , please suggest to retain the value of the variable after the loop , while IFS=: read -r line do set $dsc=$line echo 'printing line variable ' $line... (1 Reply)
Discussion started by: ParthThakkar
1 Replies

2. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

3. Shell Programming and Scripting

Read record from the text file & assign those values to variables in the script

For eg: I have sample.txt file with 4 rows of record like: user1|password1 user2|password2 user3|password3 user4|password4 The username and password is sepsrated by '|' I want to get the 1st row value from the file and assign it to two different variables(username and password) in my... (1 Reply)
Discussion started by: priya001
1 Replies

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

5. UNIX for Advanced & Expert Users

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the values... (12 Replies)
Discussion started by: manishab00
12 Replies

6. Fedora

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the... (1 Reply)
Discussion started by: manishab00
1 Replies

7. Shell Programming and Scripting

Read the csv file and assign the values in to variable

I have a csv file with the values seperated by commas.I want to extract these values one by one and assign to a variable using shell script.Any ideas or code? (11 Replies)
Discussion started by: rajbal
11 Replies

8. Shell Programming and Scripting

Read a file and assign the values to a variable

i have a file in this format curyymm PRVYYMM CDDMmmYY bddMmmyy eddMmmyy --------- ------- ------------ ---------- ----------- 0906 0905 09Jun09 01Jun09 30Jun09 ----------- --------- ------------ ------------ ----------- i need to read the... (5 Replies)
Discussion started by: depakjan
5 Replies

9. Shell Programming and Scripting

how can i read text file and assign its values to variables using shell

Hello, I have a cat.dat file, i would like shell to read each 3 lines and set this 3 lines to 3 different variables. my cat.dat is: 11 12 +380486461001 12 13 +380486461002 13 14 +380486461003 i want shell to make a loop and assign 1st line to student_id, 2nd line to... (4 Replies)
Discussion started by: rosalinda
4 Replies

10. UNIX for Dummies Questions & Answers

to assign cut values to an array

i need to seperate values seperated by delimiters and assign it to an array.. can u plz help me on that. Variables = "asd,rgbh,(,rty,got,),sroe,9034," i need to assign the variables into arrays.. like.. var=asd var=rgbh.. and so on how do i do this. i need to reuse the values stored in... (6 Replies)
Discussion started by: Syms
6 Replies
Login or Register to Ask a Question