generating the variable list for WHILE READ statement at runtime


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting generating the variable list for WHILE READ statement at runtime
# 1  
Old 09-09-2010
generating the variable list for WHILE READ statement at runtime

Hi, I am reading the contents of a file in variables as -
Code:
cat ${var_file_name} | while read COL1 COL2 COL3 COL4 COL5 COL6 COL7 COL8 COL9 COL10 COL11

The problem is ... my file can have any number of columns - 5, 10, 11 ....
So i want a dynamic variable list as -
Code:
cat ${var_file_name} | while read ${col_expr_list}

where ${col_expr_list} stores the expression like - "COL1 COL2 COL3 COL4 COL5 COL6 COL7 COL8 COL9 COL10 COL11" or "COL1 COL2 COL3 COL4 COL5" depending on the number of column in {var_file_name}

But AIX is throwing an error, when i run the script as "can not find the identifier"

Could anyone please suggest for this.....

Moderator's Comments:
Mod Comment Use code tags please.
# 2  
Old 09-09-2010
Can you get your values/fields in the input file delimeted like a csv file?
# 3  
Old 09-09-2010
yes.. they are already delimited with ':' .... and that's why i am able to populate the COL1 ... COL11 with file values.
# 4  
Old 09-09-2010
But a variable number of fields means that they are empty values in between :: like:
Code:
val1:val2:val3:val4
val1:val2::val4
val1:val2:val3:
:val2:val3:val4

Or is it like
Code:
val1:val2:val3:val4
val1:val2:val4
val1:val2:val3
val2:val3:val4

?

Because in the 1st case this would be no problem to read in. You'd just have empty variables while reading in, like in the infile. Second file would not be good to parse at all imho.
# 5  
Old 09-09-2010
say file A is having 4 fields .. so data in it would be like -
1:2:3:4
11:22:33:44
..
...
so my variable {col_expr_list} would be having - "COL1 COL2 COL3 COL4" .. and the read statement would be -

cat ${var_file_name} | while read {col_expr_list}
do
<some calculations on COL1 to COL4>
done

now say a new file B comes with 6 fields, with data as -
1:2:3:4:5:6

so my variable {col_expr_list} would be having - "COL1 COL2 COL3 COL4 COL5 COL6" .. and the output after "cat" statement should be -
COL1 = 1
COL2 = 2
COL3 = 3
COL4 = 4
COL5 = 5
COL6 = 6

but its not behaving like this .. and throwing error ...
# 6  
Old 09-09-2010
Use arrays
Code:
#!/bin/bash
IFS=':'
ROW=1
while read -a A
do
   echo "Row $ROW"
   for ((i=0; i<${#A[@]}; i++))
   do
      echo -ne "Col$i=${A[$i]}\t"
   done
   echo
   ((ROW++))
done < file

You can adapt this code for your own calculations
the ROW variable is just for displaying purpose.
# 7  
Old 09-09-2010
Thanks Frans .
I will try this and let you know
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Runtime variable extraction

Hi There, var1=value1 var2=value2 var3=value3 for (( i=1; i<4; i++ )) do temp=var$i echo "$temp" done OUTPUT var1 var2 var3 I want the output to be value1 value2 (5 Replies)
Discussion started by: jagpreetc
5 Replies

2. Shell Programming and Scripting

Evaluate Variable At Runtime

Hi, I am trying to set a variable that has time the format desired. And my intention is to echo variable (instead of actual date command) everytime I like to echo date. Please take a look at below code. $NOW='' echo $NOW After 5 minutes $echo $NOW Issue here is , I am not... (2 Replies)
Discussion started by: vinay4889
2 Replies

3. Shell Programming and Scripting

FORTRAN read statement

Hi, I've been having trouble figuring out what the following read statement is doing. DO I = 1, natom k = 3 * (i - 1) + 1 READ(8,*) AtNum(I), (X_2(J),J=k,k+2) END DO The part I don't understand is: (X_2(J),J=k,k+2) the file it is reading is simply a set of... (8 Replies)
Discussion started by: butson
8 Replies

4. Shell Programming and Scripting

Read statement not working

hello guys, i am having the below piece of code error () { echo"Press y /n" read ans case $ans in y) main;; n) exit esac } In the abve code, read statement is not working i.e not waiting for user to enter input. ,i tested exit status its 1. could anyone help me to do this ... (11 Replies)
Discussion started by: mohanalakshmi
11 Replies

5. UNIX for Dummies Questions & Answers

Read statement within while read loop

hi, this is my script #!/bin/ksh cat temp_file.dat | while read line do read test if ]; then break else echo "ERROR" fi done when i execute this code , the script does wait for the user input . it directly prints "ERROR" and terminates after the no. of times as there... (3 Replies)
Discussion started by: siva1612
3 Replies

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

7. Shell Programming and Scripting

Read SQL statement in Script

Hi Guys.. need some urgent help... I am stuck in something badly I need to write a script which would read a sql statement (which might be a join/inner join/select/sub select etc. ) I need to read that sql statement ... and in the output I want all the table names and columns (doesn't... (4 Replies)
Discussion started by: freakygs
4 Replies

8. Shell Programming and Scripting

Read from user inside a while statement

Hi there I want to ask to a user something about each line of a file. This is my code: #cat test.txt first line second line third line #cat test.sh #!/bin/ksh read_write () { echo "Do you want to print the line (YES/NO)?\n" read TEST case ${TEST} in YES) return 0;; ... (3 Replies)
Discussion started by: tirkha
3 Replies

9. Shell Programming and Scripting

Read statement not working in a script

I have a script consisting of certain functions whose input is a file at same location. In that file i have written the name of anothe file at same location. The third file contains a word which act as a function in the first script.Let me give an example i have a scrip file say 1.sh in which i am... (7 Replies)
Discussion started by: sumitdua
7 Replies

10. Shell Programming and Scripting

Generating a list of choices in a menu

Hello all, I have the below script and I'm a little stuck on the best way to continue. Essentially I'm creating a text file (systems.txt) with a list of servers in it by hostname. Then I would like to echo a menu with each hostname and a number to use to pick it from the list. It's somehow... (7 Replies)
Discussion started by: sysera
7 Replies
Login or Register to Ask a Question