How to read a delimited string and assign fields to incremented variables?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read a delimited string and assign fields to incremented variables?
# 1  
Old 07-14-2011
How to read a delimited string and assign fields to incremented variables?

How can I read a string delimited on spaces and assign the fields to incremented variables.
For example:
Given $exts= txt dat mov

I want to read in $exts and have "txt" "dat" and "mov" assigned to incremented variables like $ext1, $ext2, etc. I would like to do this in a loop so that I can read in any any given number of extensions.

I have tried the following:
Code:
#!/bin/bash
#replace
exts=txt dat mov
N=0
while x read "$exts" | cut -d" " -f1
	do
	N=$((N+1))
	echo "ext$N = $x"
done


But this does not work. Any suggestions on how to achieve the desire outcome?
Thanks.

Last edited by Franklin52; 07-14-2011 at 05:07 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 07-14-2011
Use array
Code:
#!/bin/bash
exts=( txt dat mov )
for ((i=0;i<${#exts};i++))
do 
     echo ${exts[$i]}
done

# 3  
Old 07-14-2011
This should work in any shell, no arrays required:

Code:
for X in $exts
do
        echo "X is $X"
done

# 4  
Old 07-14-2011
Quote:
Originally Posted by Corona688
..., no arrays required:
The OP want to create "incremented variables" Smilie
# 5  
Old 07-14-2011
Quote:
Originally Posted by danmero
Use array
Code:
#!/bin/bash
exts=( txt dat mov )
for ((i=0;i<${#exts};i++))
do 
     echo ${exts[$i]}
done

Thanks so much. I'll give that a try.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Read line and save fields as variables

Hej guys, I am trying to read a csv file line by line, save it's fields as variables per line so I can use them as parameters and execute stuff. I am new to shell scripting and was just strictly following a tutorial. Somehow my version seems to ignore the loop. Any help? TY! :) #!/bin/bash... (12 Replies)
Discussion started by: Splinter479
12 Replies

2. Shell Programming and Scripting

Psql output into array and read 2 fields into different variables

Hello Just edited the entry to make it easier to understand what i want How can i achieve this: GOAL: read 2 field from a table with PSQL result of this PSQL command is this INSTALLEDLANG=$(su - postgres -c "psql -A -t -q -c -d ${DBNAME} -t -c 'SELECT code, iso_code from res_lang'") ... (0 Replies)
Discussion started by: winston6071
0 Replies

3. Shell Programming and Scripting

Assign variables to CSV string (bash)

Hi guys, New to the forum, and been messing around with Linux for about a year now. I'm still very much a rookie, so just assume that I'm a total idiot: I currently have a shell that spits out a CSV number string of about 8 numbers as follows: 1.00,2.00,3.00 ... ,8.00I need to assign a... (7 Replies)
Discussion started by: hansol
7 Replies

4. Shell Programming and Scripting

Unable to read assign values to two variables in while loop

I am trying to read a input file which has two columns separated by space Input file server1 server2 server3 server4 server5 server6 When i execute the below while code it reads line by line and a and b variables are able to successfully fetch the values while read a b do echo "$a" echo... (5 Replies)
Discussion started by: chidori
5 Replies

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

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

How to read a file and assign variables to data?

Hi, I need a simple csh script to read a file containing data like this Buy Transactions : 175 Sell Transactions : 212 Server: sepo2 i want to read both field and its value and assign variables to each.. (2 Replies)
Discussion started by: pravsripad
2 Replies

8. Shell Programming and Scripting

assign colon delimited strings to variables

Man it has been too long since I have had to do this type of stuff... OK I have a file with lines in it looking like this: bob:johnson:email@email.com (most lines) john:F.:doe:email2@email.com (but some are like this) I need to loop through and assign vars to the values: var Fname =... (29 Replies)
Discussion started by: NewSolarisAdmin
29 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. Shell Programming and Scripting

Awk/shell question: Read from file and assign to variables.

Is anyone able to help with writing a program that will do the following: 1. Read the contents of a file, line by line, and on each line, assign each of the two columns to a shell variable. 2. perform an action on the variables 3. Read the next line. Here is what I've gotten so far. ... (3 Replies)
Discussion started by: akbar
3 Replies
Login or Register to Ask a Question