Read line and save fields as variables


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Read line and save fields as variables
# 1  
Old 08-05-2016
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! Smilie

Code:
#!/bin/bash
echo " I AM OUTSIDE THE LOOP"

infile="./calls.txt"

while IFS=',' read -r f1 f2
do
        echo " I AM INSIDE THE LOOP"

        foo=$f1
        bar=$f2

        #many program calls here

done < "$infile"

Execution is:
Code:
I AM OUTSIDE THE LOOP


Last edited by rbatte1; 08-08-2016 at 07:03 AM.. Reason: Changed ICODE tags to CODE tags
# 2  
Old 08-05-2016
What's inside ./calls.txt ? An empty file would just skip the loop.
# 3  
Old 08-05-2016
does 'calls.txt' reside in the SAME directory your script is at? [hint: ./calls.txt]
How do you cal you script?
What's the content of 'calls.txt'? Could you share a snippet using code tags, please.
# 4  
Old 08-05-2016
calls.txt is CSV file like:

Code:
foo1,bar1
foo2,bar2
foo3,bar3

where foo/bar are integers, strings or paths/to/things

I call the .sh with:
Code:
./HiCoPi.sh

And yes, calls.txt and the HiCoPi.sh are in the same folder

Last edited by RudiC; 08-05-2016 at 01:24 PM..
# 5  
Old 08-05-2016
Strange, I get the desired out:
Code:
 I AM OUTSIDE THE LOOP
 I AM INSIDE THE LOOP
 I AM INSIDE THE LOOP
 I AM INSIDE THE LOOP

Could you post the output of cat -vet ./calls.txt using the code tags.
# 6  
Old 08-05-2016
Maybe one of the commands in the loop tries to read from stdin? Then it reads from the loop's input; for example ssh does it; workaround is ssh -n ...
An extra descriptor can harden the while loop
Code:
while IFS=',' read -r f1 f2 <&3
do
  ...
done 3< "$infile"

This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 08-08-2016
thanks so far for your replies. I kind of have the feeling too that it is somehow related to the calls.txt
I thought the minimal example would do the job, but let's give you a more real copy of what I have done:

my.sh :
Code:
#!/bin/bash
echo " I AM OUTSIDE THE LOOP"

infile="calls.txt"

while IFS=',' read -r f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13 f14 f15 f16 f17 f18 f19
do
        echo " I AM INSIDE THE LOOP"

        my_file1=$f1
        species1=$f2
        chrom1=$f3
        my_file2=$f4
        species2=$f5
        chrom2=$f6
        binSize=$f7
        foo1=$f8
        foo2=$f9
        foo3=$f10
        foo4=$f11
        variance=$f12
        bar1=$f13
        bar2=$f14
        java_jar=$f15
        bar3=$f16
        bar4=$f17
        unit=$f18
        bar5=$f19

        # program calls here

done < "$infile"

my calls.txt is just a one liner right now, but later many lines according to the same format shall follow:
Code:
/path/to/my/file.txt,string1,string2,/path/to/my/other/file.txt,string3,string4,int1,string5,string6,int2,int3,int4,/path/to/a/third/file.txt,path/to/a/fourth/file.txt,/path/to/a/java.jar,anotherString,anotherString,anotherString,aFloatNumber

so this is exactly a renamed representation of the calls.txt. Sorry for the censorship, but in fact I am currently developing a software tool I am ofc not allowed to share everything in detail :/

I came up with the thoughts of maybe the paths messing it up? I 'll retry with quoting the paths. Other ideas?

EDIT: quoting didn't help :/
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

read line by line and calculate the co-presence of variables

Hey guyz, I have a table which shows the presence or absence of my variables (A,B,C,...) in my observations (1,2,3,...) * A B C ... 1 1 0 1 2 1 1 0 3 1 0 0 ... I want to calculate the co-presence of my variables. to have a table shows the pairwise presence of the variables (have... (1 Reply)
Discussion started by: @man
1 Replies

3. UNIX for Dummies Questions & Answers

Set variables from fields in fields

Hi, This is my first post here and I am a newbie. :) I have a file that looks like this : Introduction:Intro_123.html Product definition:Prod_def.html System Setup:SSetup-64bit.html Setting up user accounts:Set_user_acc.html I tried to create a script that would output "The filename... (3 Replies)
Discussion started by: Joq
3 Replies

4. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: runit
4 Replies

5. Shell Programming and Scripting

awk (or other) script that assigns fields from a line to multiple variables

Hey all, Unfortunately I have only basic knowledge of awk and/or scripting. If I have a file with lines that can look similar to this: Name=line1 Arg1=valueA Arg2=valueB Arg3=valueC Name=line2 Arg1=valueD Name=line3 Arg1=valueE Arg3=valueF Name=line4 Arg2=valueG ... (4 Replies)
Discussion started by: Rike255
4 Replies

6. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

7. Shell Programming and Scripting

Read 1-line file and separate into multiple variables

I have one line files with 17 records separated by a semi-colon. I need to create a variable from each record, which I can do via a separate awk for each one, but I know there has to be a better way. Along with pulling out the variable, I need to convert some url coding like a + to a space, etc.... (4 Replies)
Discussion started by: numele
4 Replies

8. Solaris

Save enviroment variables

I need to save my enviroment variables,specially the $PATH.When I put it on .cshrc at next reboot I lost the configuration.How can avoid this?Thanks (2 Replies)
Discussion started by: bgf0
2 Replies

9. Shell Programming and Scripting

Read variables from line to fixed length

I would like to make a script to read three variables (no fixed length or position) from a line and write them into a file, with fixed length and right-justified in each column. The fixed text (text1-text4) prior to the thee variables and the variables themselves are originally separated by spaces... (3 Replies)
Discussion started by: SharkM
3 Replies

10. Shell Programming and Scripting

How to read a line and put it into 3 variables

Hi All, I'll get a file whose 2nd line contains 3 fields: filename(variable length), file size char(10), and record count int(10). How do I cut it and put it into 3 variables? eg: abcd.csv01234567891111111111 now I want: $one = abcd.csv, $two = 0123456789, $three = 1111111111. I also... (8 Replies)
Discussion started by: Mandab
8 Replies
Login or Register to Ask a Question