Extract information from txt file

Hello!
I need help :slight_smile: I have a file like this:

AA BC FG

RF TT GH

DD FF HH

(a few number of rows and three columns) and I want to put the letters of each column in a variable step by step in order to give them as input in another script. So I would like to obtain:

for the 1� loop: var1 = AA var2 = BC var3 = FG

for the 2� loop: var1 = RF var2 = TT var3 = GH

for the 3� loop: var1 = DD var2 = FF var3 = HH

Thank you in advance. Best regards :slight_smile:

Welcome edekP,

I have a few to questions pose in response first:-

  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)

Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

We're all here to learn and getting the relevant information will help us all.

Kind regards,
Robin

Dear Robin,
thank you for your reply!
No, it isn't a homework, I am starting to use bash script so I have some problem :frowning:
I know that I can print a value of a column with awk + print option, but I don't know how to select the row and how to "put" the values in a variables.
I think that maybe the best way for doing it is to use a "for" loop and with awk print the column $1 $2 $3.

Thank you in advance for your help.
Best.

Can you try the below one ?

 while read var1 var2 var3 ; do echo var1:$var1 var2:$var2 var3:$var3; done < file 

Yes, It works! Could you explain me how work "while read"?
How does it understand to put the values in the variables?
I know that maybe it is a stupid question but I have never used that command.
Thank you in advance!

Hi,

Hope this helps.

1 Like

Sorry, I have another question...
if the file has different number of column how can I do it?
I saw that with while read line it is possible to read all line, but how can I define a number of variable that change with the number of column?
For example if the file is:

AA FG TH
AD FR HK LO
AX DF ER TR
AZ SD WE

Thank you in advance.

Did you consider read ing an array? That would require a recent shell, though, your version of which you failed failed to mention.

$ cat tst.cfg
AA BC FG
RF TT GH NL
DD FF HH
$ cat mtst
#!/bin/bash
while read a
do set -- $a
i=0
while [ $i -lt $# ]
do let i=i+1
eval echo -n var$i=\$$i'\ '
done
echo
done <tst.cfg
$ sh mtst
var1=AA var2=BC var3=FG
var1=RF var2=TT var3=GH var4=NL
var1=DD var2=FF var3=HH
$

Feel free to adjust the code to your platform & needs

If you are sure of just three fields, you could:-

while read var1 var2 var3 extra
do
   echo "\$var1=$var1"
   echo "\$var2=$var2"
   echo "\$var3=$var3"
   if [ ! -z "$extra" ]
   then
      echo "I have extra fields \"$extra\""
   fi
done < input_file

You would need to work with the variables within the loop and depending on your shell, the last values set may or may not persist after the loop terminates (i.e. it may read nulls into the variables as it tries to read beyond the end of file.

I hope that this helps,
Robin

You can try the below one as well ( in bash ) .

while read -a line; do for i in ${line[@]}; do echo -n "var$x:$i "; ((x++)); done; echo "";x=1;done < file

With a recent shell:

while read -a VARR; do for i in ${!VARR[@]}; do echo -n "var$i:${VARR[$i]} "; done; echo;  done < file3
var0:AA var1:FG var2:TH 
var0:AD var1:FR var2:HK var3:LO 
var0:AX var1:DF var2:ER var3:TR 
var0:AZ var1:SD var2:WE 
1 Like