(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:
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.
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
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.
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!
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:
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.
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