Split string with blancs to pick up information in a file ?

Hello,

I am quite new in shell, and would like to pick up information in a file.

The file structure is like this faor all data:

T 50 2 2.5      is this a candy  number  color  price

I know how to pick up a line. I do this:

head -linenumber candyfile.doc | tail -1

But I would like to know how to split this line to pick up data.

For exemple I need the price at the second line or the number at the fifth, etc...

How can I split with blanck and writ data in a kind of log file like:

candyfile1.doc : data1 : data2 : data3
candyfile1.doc : data1 : data2 : data3

etc...

thanks for your precious help !

I can't see any relation between the data you're getting in and the data you want out, but splitting on spaces is easy:

while read A B C D E F G H
do
          echo "First field is $A"
          echo "Second field is $B"
          ...
done < inputfile > outputfile

your solution don't seem to work:

shadok@pomputer:~$ a=head -3 candyfile.doc | tail -1
shadok@pomputer:~$ echo $a
T 50 2 3.5   is this a candy color price
pomputer:~$ while read $a; do echo "First field is $A"; done

First field is 
^C
shadok@pomputer:~$ while read a; do echo "First field is $A"; done

First field is 

So, Where is the problem in what I tried ?

The <inputfile is not optional. Without that, it tries to read from the keyboard instead.

Also, "a" is not the same as "A".

Also, if you list only one field, it will cram everything into that one field. List all the fields instead.

something like this:

awk '{print FILENAME , $(NF-2),$NF}' OFS=":" infile

Ok,

I can't use these syntax since I need only some lines of my file and on each line some arguments.

So, I just want to know how to split with the blanks, the line that was read and stored in a variable.

if I have

 a= 12 2 51 6.2 tata titi toto tutu

How can I split a to obtain for exemple the 4th variable 6.2 or the second 2 ?

Sorry if my question wasn't clear...

a="12 2 51 6.2 tata titi toto tutu"
echo $a |awk '{print $2}' 
echo $a |awk '{print $4}'