Reading multiple values in while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading multiple values in while loop
# 1  
Old 03-20-2013
[Solved] Reading multiple values in while loop

I'm having trouble with a simple piece of code.
Code:
IFS=,
echo "1,2,3,4,5,6,7,8" | while read x y
do
echo "x=$x"
echo "y=$y"
done

I'm hoping for
x=1
y=2
x=3
y=4
.
.
.

but I'm getting
x=1
y=2,3,4,5,6,7,8

what am I missing here?

Last edited by vbe; 03-20-2013 at 02:08 PM.. Reason: code tags
# 2  
Old 03-20-2013
Hi, execute this
Code:
IFS=,
echo "1,2
3,4
5,6
7,8" | while read x y
do
echo "x=$x"
echo "y=$y"
done

This User Gave Thanks to franzpizzo For This Post:
# 3  
Old 03-20-2013
read is a line reading tool and the last value read in one iteration of the command is treated as the remainder of the line even if contains field separators. Fortunately you can change the line delimiter with the -d option:

Code:
IFS=,
echo "1,2:3,4:5,6:7,8" | while read -r -d ':' x y
do
echo "x=$x"
echo "y=$y"
done

Mike

PS. read is capable of escaping characters and you can turn this off with -r. As a general rule, you should always use -r unless you have a reason not to.

Last edited by Michael Stora; 03-20-2013 at 02:36 PM..
These 2 Users Gave Thanks to Michael Stora For This Post:
# 4  
Old 03-20-2013
Thanks

Thanks to both for the quick reply.

Newline in the input wasn't an option with my real life data, but Michael Stora's suggestion solved my problem.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Reading multiple variables in a loop

Hi, I managed to read and print variable as shown in the below code. table_name=table1,table2,table3 i=0 IFS="," for i in $table_name do echo $i done Is there a way how I can read more than one variable. For example I need to read 2 variables and populate the output... (6 Replies)
Discussion started by: shash
6 Replies

2. Shell Programming and Scripting

Reading multiple values from multiple lines and columns and setting them to unique variables.

Hello, I would like to ask for help with csh script. An example of an input in .txt file is below, the number of lines varies from file to file and I have 2 or 3 columns with values. I would like to read all the values (probably one by one) and set them to independent unique variables that... (7 Replies)
Discussion started by: FMMOLA
7 Replies

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

4. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

5. Shell Programming and Scripting

While loop reading file with multiple conditions

Hi Am trying to print the PIDs of process in a file and trying to grep any PID from that file I set the if condition as $value != "PID" and $value != "-" Assign that number to a variable Am confused since am using while loop to read the line from file and again if condition to check those... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

6. Shell Programming and Scripting

[SOLVED] UNIX FOR loop to read a variable with multiple values

Hi, I have a variable which stores file names as a result of find command. I need to delete all these files one by one, i.e. by a loop. Can anyone tell me how can it be done? The variable f2d has the file names like these abc.txt bcd.txt fff.txt gef.txt Now I have used a loop as... (12 Replies)
Discussion started by: jhilmil
12 Replies

7. Shell Programming and Scripting

Return multiple values using for loop in perl

I am using a for loop to copy files from say DIR1 and DIR2 to DIR3.I have to check whether files are copied from DIR1 and DIR2 and print the respective message. @path=("$DIR1","$DIR2"); foreach (@path) { $rc=system("cp $_/*xml $DIR3"); if ($rc == 0) { print "Files were copied... (1 Reply)
Discussion started by: liyakathali
1 Replies

8. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

9. Shell Programming and Scripting

reading the values from a file in C Shell for loop

Hi All, I need small help on for loop syntax in C shell. How can we read the values from a file (line by line) through C shell loop. For Ex: $Cat file1 data1 data2 data3 data4 $ I have to print those values in a variable and have to perform some steps... Can anyone help on... (2 Replies)
Discussion started by: raghu.iv85
2 Replies

10. Shell Programming and Scripting

Using multiple values for single variable in a loop

Hello Guys, I have a small loop problem as below. I have 3 different values to be used while running the same script - va1="some-value1" va2="some-value2" va3="some-value3" Now I want to use these three variable values to be used for running the same command, like - while... (1 Reply)
Discussion started by: rockf1bull
1 Replies
Login or Register to Ask a Question