How to fetch values from a line in a file to variables in UNIX?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to fetch values from a line in a file to variables in UNIX?
# 1  
Old 08-14-2013
How to fetch values from a line in a file to variables in UNIX?

Hi,

I need to assign values from a lines in a file into variables in unix, i am using Korn shell.

I tried the below script from posts but i am unable to fetch every value in a variable.

Code:
#! /usr/bin/ksh

#for file in test.txt; do
IFS=$'\|'
I=1
while read -a val
do
echo "val[${I}]=${val[${I}]}"
I=` expr ${I} + 1 `
done < fs_mail.txt
#done

but i am getting error.

Code:
test_script_2.ksh[7]: read: -a: unknown option
Usage: read [-ACprsv] [-d delim] [-u fd] [-t timeout] [-n nchar] [-N nchar] [var?prompt] [var ...]

I tried this it worked but my file is pipe | separated ,variable are not as i expected.

Code:
#!/usr/bin/ksh
I=1
for WORD in $(cat test.txt); do
WORDS[$I]=$WORD
echo "WORDS[$I]=$WORD"
I=$((I + 1))
done

File will look like

Code:
line1|111|22|33|44|55|aa|bb|cc
line2|dd|ee|ff|gg|ii|jj|kk|ll|mm|nn|oo|pp

Could anyone Please advise, how to achive.

Thanks,
Regards,
karthikram
# 2  
Old 08-14-2013
To start read -a is bash syntax. The equivalent ksh syntax is read -A
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 08-14-2013
Hi Scrutinizer,

Thanks for the quick reply read -A worked well for korn shell , i am unable to get the code properly. How to frame the variable loop to get each value from a line in a file.

like :
Code:
LINE_1_VALUE_1=111
LINE_2_VALUE_2=22
...
..
END OF LINE
LINE_2_VALUE_1=dd
LINE_2_VALUE_2=ee
...
...
..

Please give a gist or an idea/approach, if possible.

Thanks,
Regards,
karthikram

---------- Post updated at 10:45 AM ---------- Previous update was at 09:45 AM ----------

Hi,

i used IFS=$'\|' after that i able to find the script passing the individual value from a line in file to variable, but i need to read the end of the line , no sure how to , could anyone advise.

Code:
#!/usr/bin/ksh
IFS=$'\|'
I=0
for WORD in $(cat fs_mail.txt); do
WORDS[$I]=$WORD
echo "WORDS[$I]=$WORD"
I=$((I + 1))
done

Thanks,
Regards,
karthikram

Last edited by Scott; 08-23-2013 at 01:23 PM.. Reason: Code tags for data too
# 4  
Old 08-14-2013
This is a solution in bash, but you surely can devise a similar ksh thingy:
Code:
while IFS=\| read -a WORD; do echo -e ${#WORD[@]} "\t" ${WORD[@]}; done < file
9      line1 111 22 33 44 55 aa bb cc
13      line2 dd ee ff gg ii jj kk ll mm nn oo pp

This User Gave Thanks to RudiC For This Post:
# 5  
Old 08-15-2013
Hi , Thanks for looking into my request, Please help how to make loop inside the While loop to get the variables line by line , nothing is working for me.

In my above script it is taking the whole file into a variable and array element contains but differentiating between end of line and next line is not doing.

I am able to understand how many values does each line has using ${#WORD[@]}, Could any one please help how achive.

eg:
Code:
LINE_1[0]=AA
LINE_1[1]=BBB
.. 
..
LINE_2[0]=11
LINE_2[1]=22
..
..

for each line i need to create variable.

Thanks,
Regards,
karthikram

Last edited by Scott; 08-23-2013 at 01:23 PM.. Reason: Code tags
# 6  
Old 08-15-2013
I used your file from post#1, and in my snippet WORD[0] = "line1; WORD[1] = "111", and so on. So - what else do you need?
# 7  
Old 08-19-2013
Hi,

Thanks for workaround, Actually my requirement is to get values from line by line in a file and assign to variable, it is good that for each line if there is separate variable also, from the above idea provided.

That is

initially my file looks like this
Code:
111|22|33|44|55|aa|bb|cc
dd|ee|ff|gg|ii|jj|kk|ll|mm|nn|oo|pp
 
line1[0]=111
line1[1]=22
line1[2]=33
line1[3]=44
line1[4]=55
line1[5]=aa
line1[6]=bb
line1[7]=cc
 
line2[0]=dd
line2[1]=ee
line2[2]=ff
line2[3]=gg
line2[4]=ii
line2[5]=jj
line2[6]=kk
line2[7]=ll
line2[8]=mm
line2[9]=nn
line2[10]=oo
line2[11]=pp

I tried to fetch using the below script but, it is not getting in separate variable for each line instead , in single variable array has the entire file data.

script:
Code:
#!/usr/bin/ksh
IFS=$'\|'
I=0
for WORD in $(cat fs_mail.txt); do
WORDS[$I]=$WORD
echo "WORDS[$I]=$WORD"
I=$((I + 1))
done


but the output like :
Code:
WORDS[0]=111
WORDS[1]=22
WORDS[2]=33
WORDS[3]=44
WORDS[4]=55
WORDS[5]=aa
WORDS[6]=bb
WORDS[7]=cc
dd
WORDS[8]=ee
WORDS[9]=ff
WORDS[10]=gg
WORDS[11]=ii
WORDS[12]=jj
WORDS[13]=kk
WORDS[14]=ll
WORDS[15]=mm
WORDS[16]=nn
WORDS[17]=oo
WORDS[18]=pp

As Mr.RudiC provided snippet to get the number of values in the line using that how to make a loop to create each variable for each line.

Could anyone Please help me on this. I hope i have explained it correctly.

Thanks,
Regards,
karthikram

Last edited by Scott; 08-23-2013 at 01:24 PM.. Reason: Code tags for data too
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fetch the values based on a Key using awk from single file

Hi, Please help to fetch the values for a key from below data format in linux. Sample Input Data Format 11055005|PurchaseCondition|GiftQuantity|1 11055005|PurchaseCondition|MinimumPurchase|400 11055005|GiftCatalogEntryIdentifier|Id|207328014 11429510|PurchaseCondition|GiftQuantity|1... (2 Replies)
Discussion started by: mohanalakshmi
2 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. UNIX for Dummies Questions & Answers

Getting values of 2 columns from sql query in UNIX variables

Hi, I have connected to oracle database with sqlplus -s / <<EOF select ename, age from emp where empid=1234; EOF I want to save the values of ename and age in unix shell variables. Any pointers would be welcome.. Thanks in advance!!1 Cheers :):):):) (1 Reply)
Discussion started by: gonchusirsa
1 Replies

4. Shell Programming and Scripting

Fetch Data from File using UNIX or Perl

Hello, How All are Doing today. I have a issue, I have a file which contains the data as follow <ENVELOPE><ENVELOPE_ID>TEST</ENVELOPE_ID><ENVELOPE_EXTERNAL_ID></ENVELOPE_EXTERNAL_ID><ENVELOPE_VERSION>2</ENVELOPE_VERSION><SIResourceDefaultVersion>true</SIResourceDefaultVersion><TYPE>GS... (1 Reply)
Discussion started by: adisky123
1 Replies

5. Shell Programming and Scripting

How to fetch File from a URL to Unix Server?

Hello All, I wanted to get the software to be fetched from the Service Provide URL to my unix server. I tired using the mget, but resulted in error. Please take a look. $ wget -O V3-0-5-2.Solaris8-SPARC.tar.gz --http-user=hd87es3 --http-passwd=987dnja7 http://beyond.abinitio.com... (3 Replies)
Discussion started by: raghunsi
3 Replies

6. Shell Programming and Scripting

fetch last line no form file which is match with specific pattern by grep command

Hi i have a file which have a pattern like this Nov 10 session closed Nov 10 Nov 9 08:14:27 EST5EDT 2010 on tty . Nov 10 Oct 19 02:14:21 EST5EDT 2010 on pts/tk . Nov 10 afrtetryytr Nov 10 session closed Nov 10 Nov 10 03:21:04 EST5EDT 2010 Dec 8 Nov 10 05:03:02 EST5EDT 2010 ... (13 Replies)
Discussion started by: Himanshu_soni
13 Replies

7. Shell Programming and Scripting

How to fetch a specific line from file

Hi, I have text file in the following strucher . The files contain hondreds of lines. value1;value2;value3;value4 I would like to get back the line with lowest date (values4 field). In this case its line number 3. groupa;Listener;1;20110120162018 groupb;Database;0;20110201122641... (4 Replies)
Discussion started by: yoavbe
4 Replies

8. Shell Programming and Scripting

[BASH] mapping of values from file line into variables

Hello, I've been struggling with this for some time but can't find a way to do it and I haven't found any other similar thread. I'd like to get the 'fields' in a line from a file into variables in just one command. The file contains data with the next structure:... (4 Replies)
Discussion started by: semaler
4 Replies

9. Shell Programming and Scripting

How to fetch data from a text file in Unix

I want to fetch passwords from a common file xxxx.txt and use it in a script. Currently the password is hardcoded so this have to be changed so that password can be fetched from text file..... Please reply asap.. Thanks (4 Replies)
Discussion started by: shikhakaul
4 Replies

10. Programming

how do u assign the values to different variables when it is presneted in one line??

hey people.. i have a configuration file that looks like 7080 7988 net04.xxxxx.edu 20 where 20 is the number of threads in the thread pool initially. net04.xxxxx.edu is the hostname. and 7080 7988 are two ports. first one for client requests and second one for dns communication. now my... (2 Replies)
Discussion started by: SwetaShah
2 Replies
Login or Register to Ask a Question