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
# 8  
Old 08-08-2016
If you want to read 19 values from a file I have no idea why you don't read those values from the file instead of using unneeded intermediate variables:
Code:
#!/bin/bash
echo " I AM OUTSIDE THE LOOP"

infile="calls.txt"

while IFS=',' read -r my_file1 species1 chrom1 my_file2 species2 chrom2 binSize foo1 foo2 foo3 \
    foo4 variance bar1 bar2 java_jar bar3 bar4 unit bar5
do
        echo " I AM INSIDE THE LOOP"

        # program calls here

done < "$infile"

But, of course, neither the way you were doing it nor what I have shown above does anything that shows the values of any of the 19 variables read from line in calls.txt.

What makes you think your code or the code above is not working? Do you actually have any code in place of:
Code:
        # program calls here

that actually tries to print those values? The only thing you have shown us in this script that produces (or should produce) any output is the two echo statements (which shows us that despite you saying that calls.txt contains one line, it appears that your script is reading three lines from it).
# 9  
Old 08-08-2016
the point is, I can't even echo variables.

I get
Code:
> bash HiCoPi.sh 
I AM OUTSIDE THE LOOP

It seems he never enters the while loop. this is why the stuff in the "program calls"
section never runs.
# 10  
Old 08-08-2016
once again, please show the output of cat -vet ./calls.txt using code tags.
# 11  
Old 08-08-2016
calls.txt:
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

# 12  
Old 08-08-2016
If the output you showed us in post #11 was the output you get from the command:
Code:
cat -vet calls.txt

then the problem is that there is no <newline> character at the end of the file. The read command reads lines of input until it hits EOF, and without a <newline> character there is no line.

By definition, non-empty text files end with a <newline> character. By convention, a file with the .txt filename extension should be a text file.

If you are using an editor that creates files in DOS text file format (with <carriage-return><newline> two character line separators and no last line terminator), it is going to cause you headaches whenever you try to use those files on UNIX-like operating systems. Ffix your input file(s) so that they use UNIX <newline> single character line terminators. Then try our suggestions again.

NOTE that the text files containing the code we have suggested MUST also be UNIX format text files; not DOS format text files.
This User Gave Thanks to Don Cragun For This Post:
# 13  
Old 08-09-2016
Don Cragun Sir, you deserve a medal! That's it! I had no end of line character!

I used sublime text, like I always do for coding. Maybe the editor has some formatting default with the .txt ending, I don't know.
My solution: I put the same line from the former calls.txt into a file just called "calls" (no ending), created with nano.
Now I have a "$" as line ending character, somehow not intuitive for me, I was expecting "\n" but it works anyway. My <#program calls here> section gets executed. Fantastic!

SmilieSmilie
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