0

Trying to loop on a file that contains (see below) but it's not individually taking it per line per row.

7b.01.10  0  0.00   0.00   ....  .    0.00  ....  .
3c.00.2   0  0.00   0.00   ....  .    0.00  ....  .
7b.01.0   0  40.18  40.18  1.00  134  0.00  ....  .
3c.00.3   0  35.65  35.65  1.00  135  0.00  ....  .
7b.01.1   0  28.30  28.30  1.00  133  0.00  ....  .
3c.00.4   0  44.71  44.71  1.00  133  0.00  ....  .
7b.01.2   0  32.82  32.82  1.00  131  0.00  ....  .
3c.00.5   0  40.75  40.75  1.00  134  0.00  ....  .
7b.01.3   0  37.92  37.92  1.00  137  0.00  ....  .
3c.00.6   0  32.82  32.82  1.00  133  0.00  ....  .
7b.01.4   0  30.56  30.56  1.00  138  0.00  ....  .

what I want to process is the value in each of the lines. For example, the first line.

F1=7b.01.10
F2=0
F3=0.00
F4=0.00
F5=....
F6=.
F7=0.00
F8=....
F9=.

The reason is that I will be using each variable for another if statement within the loop. Ex: ans=F1*F4

for i in `cat file.txt`; do
        F1=`echo "${i}" | awk '{ print $1 }'`
        F2=`echo "${i}" | awk '{ print $2 }'`
        F3=`echo "${i}" | awk '{ print $3 }'`
        F4=`echo "${i}" | awk '{ print $4 }'`
        F5=`echo "${i}" | awk '{ print $5 }'`
        F6=`echo "${i}" | awk '{ print $6 }'`
        echo "$F1 $F2 $F3 $F4 $F5 $F6 "
done

any idea what am i missing?

gafm
  • 351
  • 1
  • 9
  • Does this answer your question? [Looping through the content of a file in Bash](https://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash) – kerolloz Mar 01 '22 at 12:19
  • @kerolloz, I actually need to store each string to a var for another `if case`. ex: `a=7b.01.10, b=0, c=0.00,d=0.00,e=....,f=.,g=0.00` – gafm Mar 01 '22 at 12:24
  • Word splitting is done by default on white space. This can be changed by setting `IFS` to, i.e., a newline character, but in your case, it assumes the default, which means is that `i` successively takes on each **word** from the file. Have a look at the section _Shell Variables_ in the bash man page, which also describes `IFS`. – user1934428 Mar 01 '22 at 12:28
  • It's extremely likely that you've gone down the rabbit hole and should be doing whatever it is you're doing in a single call to awk instead of involving a shell loop (see [why-is-using-a-shell-loop-to-process-text-considered-bad-practice](https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice)). Post a new question if you'd like help with how to do whatever it is you're trying to do instead of how to implement your current approach. – Ed Morton Mar 01 '22 at 13:28
  • Also see [Read a file line by line assigning the value to a variable](https://stackoverflow.com/q/10929453/4154375). – pjh Mar 01 '22 at 13:33
  • [Shellcheck](https://www.shellcheck.net/) identifies several problems with the code. – pjh Mar 01 '22 at 13:34
  • Unfortunately, most of the code in old Stackoverflow questions relating to reading files line by line in Bash is bad. Good code, and lots of useful information, can be found in [BashFAQ/001 (How can I read a file \(data stream, variable\) line-by-line \(and/or field-by-field)?)](https://mywiki.wooledge.org/BashFAQ/001). – pjh Mar 01 '22 at 13:44

3 Answers3

1
#!/bin/bash

input="data.txt"
while IFS= read -r line
do
        read -ra array <<< "$line"
        F1="${array[0]}"
        F2="${array[1]}"
        F3="${array[2]}"
        F4="${array[3]}"
        F5="${array[4]}"
        F6="${array[5]}"

        echo "$F1 $F2 $F3 $F4 $F5 $F6 "

done < "$input"
Mark Reed
  • 91,912
  • 16
  • 138
  • 175
rabbit
  • 95
  • 7
  • Thanks @rabbit, but it's not taking and saving the desired string per row. should be be `F1=7b.01.10 F2=0, F3=0.00, F4=0.00, F5=....` instead this is the output from the command. `{7b.01.10[0]} {7b.01.10[1]} {7b.01.10[2]} {7b.01.10[3]} {7b.01.10[4]} {7b.01.10[5]} {3c.00.2[0]} {3c.00.2[1]} {3c.00.2[2]} {3c.00.2[3]} {3c.00.2[4]} {3c.00.2[5]}` – gafm Mar 01 '22 at 13:03
  • Please update your question to state that you only want the first value on each line. ``` for i in `cat file.txt`; do ``` process a line at a time. Will there only ever be 6 lines in the file? – rabbit Mar 01 '22 at 13:09
  • 2
    @gafm: There was a typo in @rabbit's answer; the assignments should have `${array[i]}`, not `{$array[i]}`. Since it was a simple transposition typo I've taken the liberty of correcting it. – Mark Reed Mar 01 '22 at 13:16
  • Thanks @MarkReed. I've updated my question for clarity. I still can't get the right solution. It's still showing the same output. – gafm Mar 01 '22 at 13:22
  • 1
    @gafm. Mark Reeds edit works. here is the output. 7b.01.10 0 0.00 0.00 .... . 3c.00.2 0 0.00 0.00 .... . ... – rabbit Mar 01 '22 at 13:26
0

This Shellcheck-clean code handles a common issue with text data files:

#! /bin/bash -p

inputfile='data.txt'
while read -r f1 f2 f3 f4 f5 f6 f7 f8 f9 || [[ -n $f1 ]]; do
    printf '%s\n' "$f1 $f2 $f3 $f4 $f5 $f6 $f7 $f8 $f9"
done < "$inputfile"
pjh
  • 6,388
  • 2
  • 16
  • 17
-2

You might want to use read

while read line; do
       echo $line
done < file.txt

This should print the file line by line. So, now, you get to do your magic with each line .

kerolloz
  • 338
  • 2
  • 16
  • That should be `while IFS= read -r line; do printf '%s\n' "$line"; done < file.txt`. As written it'd corrupt the input when reading it and corrupt the output when writing it for various input/output values. – Ed Morton Mar 01 '22 at 13:30
  • [Shellcheck](https://www.shellcheck.net/) identifies the missing `-r` and missing quotes issues. See [Why is printf better than echo?](https://unix.stackexchange.com/q/65803/264812) for the `echo` issue. – pjh Mar 01 '22 at 13:39
  • Good code, and good explanations of it, can be found in [BashFAQ/001 (How can I read a file \(data stream, variable\) line-by-line \(and/or field-by-field)?)](https://mywiki.wooledge.org/BashFAQ/001). – pjh Mar 01 '22 at 13:45