Read all the fields in a Loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read all the fields in a Loop
# 1  
Old 11-01-2012
Read all the fields in a Loop

Hi,

I have a line like
A,B,C,D,E,F

and I need to loop for 6 times (as many number of fields in above line) and in every loop I need to get the value into a variable. How can I do it?

Pls advise!

Thanks much!!
# 2  
Old 11-01-2012
Code:
while IFS=',' read var
do
   echo $var
done < infile

Note: Replace infile with your file name which has line like A,B,C,D,E,F
# 3  
Old 11-01-2012
Hi

Code:
$ cat file
A,B,C,D,E,F

Code:
$ sed 's/,/\n/g' file | while read var
> do
>   echo $var
> done

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 4  
Old 11-01-2012
Quote:
Originally Posted by bipinajith
Code:
while IFS=',' read var
do
   echo $var
done < infile

Note: Replace infile with your file name which has line like A,B,C,D,E,F
Sorry It does NOT work - Simply prints the line as it is (with commas) - Am using bash.

Thanks anyway!!

---------- Post updated at 12:50 PM ---------- Previous update was at 12:50 PM ----------

Quote:
Originally Posted by guruprasadpr
Hi

Code:
$ cat file
A,B,C,D,E,F

Code:
$ sed 's/,/\n/g' file | while read var
> do
>   echo $var
> done

Guru.
Works great - Thanks much!!
# 5  
Old 11-01-2012
My bad, that was incorrect way of using it. Correction:-

Code:
while IFS=',' read v1 v2 v3 v4 v5 v6
do
   echo "$v1 $v2 $v3 $v4 $v5 $v6"
done < infile

But I guess you won't prefer this since it is fetching values into multiple variables.
# 6  
Old 11-01-2012
Quote:
Originally Posted by bipinajith
My bad, that was incorrect way of using it. Correction:-

Code:
while IFS=',' read v1 v2 v3 v4 v5 v6
do
   echo "$v1 $v2 $v3 $v4 $v5 $v6"
done < infile

But I guess you won't prefer this since it is fetching values into multiple variables.
I won't use it as it uses multiple variables and also there can be ANY number of fields.

Thanks
# 7  
Old 11-01-2012
Code:
OLDIFS="$IFS"
IFS="," # Split unquoted variables on comma instead of spaces

VAR="a,b,c,d,e,f,g"

for X in $VAR
do
        echo $X
done

IFS="$OLDIFS"

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

Can ksh read records with blank fields

I have a tab delimited file with some fields potentially containing no data. In ksh 'read' though treats multiple tabs as a single delimiter. Is there any way to change that behavior so I could have blank data too? I.e. When encountering 2 tabs it would take it as a null field? Or do I have to... (3 Replies)
Discussion started by: benalt
3 Replies

4. Shell Programming and Scripting

awk Loop 4 digit fields

Can somebody help me on this script with a loop? gawk --re-interval '{if(($1 ~ /]{4}/) && ($2 ~ /]{4}/) && ($3 ~ /]{4}/)) print $0}' I would like to loop until NF and while $i does not match ($i ~ /]{4}/) anymore then print all matching $i and exit. (5 Replies)
Discussion started by: sdf
5 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

KSH Programming to read and mail fields

I have a file with the following values: File name à a.log (bulk file with 100+ lines with the similar format) aaaa|bbbb|cccc|dddd|eeee|ffff|gggg|hhhh|iiii| aaaa|bbbb|cccc|dddd|eeee|ffff|gggg|hhhh|iiii| aaaa|bbbb|cccc|dddd|eeee|ffff|gggg|hhhh|iiii|... (3 Replies)
Discussion started by: shivacbz
3 Replies

7. UNIX for Dummies Questions & Answers

How do I read/find/replace fields in a csv datafile?

hello. I'm somewhat a novice here so please be patient. My stumbling block when loading csvs into ORACLE tables is this: I need to read a csv datafile, check several fields in each line, and if any of stated fields contain A ZERO only then replace it with a null/blank character. I had a... (9 Replies)
Discussion started by: MrCarter
9 Replies

8. Shell Programming and Scripting

How to read and compare multiple fields in a column at the same time

Hi, Currently I am coding up a nasty way of reading file input using *cat* rather than *read*. My text input looks like TextA 100 TextB 110 TextC 120 Currently I am using cat |while read line to read the first column and second column fields. cat foo.txt|while read line do ... (1 Reply)
Discussion started by: ahjiefreak
1 Replies

9. Shell Programming and Scripting

need for loop to include fields as one line

another question that could possibly be answered with awk... #cat filename ab cd ef:ghi:jk lm:nop qrs ab cd ef:ghi:jk lm:nop qrs ab cd ef:ghi:jk lm:nop qrs ab cd ef:ghi:jk lm:nop qrs # for x in `awk 'sub($1" +"$2" +","",$0) ' filename`; do echo $x; done ef:ghi:jk ... (8 Replies)
Discussion started by: prkfriryce
8 Replies

10. Shell Programming and Scripting

AWK create loop for fields

Hi everybody: I just create awk script. This script calculate the average of a field, but i want to do it for every fields. The original field has 40 fields of numbers like that: 38.00 51.00 10.00 -99.90 75.00 47.00 4.00 -99.90 69.00 121.00 62.00 6.00 70.00 43.00 36.00 49.00 8.00 36.00 50.00... (1 Reply)
Discussion started by: tonet
1 Replies
Login or Register to Ask a Question