Reading a table in a shell script

Dear all:

I want to write a script capable of reading specific rows and collumns of a table, into a variable.

Just imagine i have a file named table.dat which contains:

GENERAL INFORMATION
Col 1 Col2 Col3
1 1
2 2
3 3
4 4

What i want to do is reading, for each line, the data contents of col1 and col 2 (assigning them to variables, instead of writing them to files), in order to do some processing with those results.

Does anyone have a clue on how to accomplish this task?
Many thanks in advance

Luis Carvalheiro

One basic shell construct for doing this is:

while read COL_1 COL_2 COL_3
do
    <some code using your read variables COL_1, COL_2, COL_3>
done < table.dat

You can do something like this:

while read col1 col2
do
    echo "Col1 : $col1"
    echo "Col2 : $col2"
done < table.dat

Jean-Pierre.

I whish to thank the speed of your comments. I followed those instructions but I think i missed something. To make my intempts clear, i'll copy a general "table" whose contents i wish to read (a kind of cut&paste, basically)

---> table start
This is the table title, which doesn't need to be read (9)

COLUMN LINE COUNT :

1632    531    52.000
1641    617    52.000
1651    604    52.000
1651    605    52.000
1652    601    52.000
1652    604    52.000
1652    605    51.000
1652    606    52.000

----> table end

Ok. what i want is to read sequentially the numbers in COLUMN and LINE, i.e., 1632, 531; 1641, 617, and so on. For the time being, i don't care about the COUNT value.

I'm sorry if this sounds like a lame question...
Thankfull:

Luis

and what exactly in the posted solutions did not work?

Your text needs to have some known attributes such as a colon after your column headings for this to work:

{
IGNORE_FL=T
while read COLUMN LINE COUNT
do
    # Ignore blank lines
    if [[ ${#COUNT} -eq 0 || ${COLUMN} = --* ]]; then
        continue
    fi

    # COUNT variable will have the colon description terminator (":"), set IGNORE_FL to F and skip line
    if [[ ${COUNT} = *: ]]; then
        IGNORE_FL=F
        continue
    fi

    # Ignore all lines until actual columns are found
    if [[ ${IGNORE_FL} = T  ]]; then
        continue
    fi

    print "Column = ${COLUMN} / LINE = ${LINE}" 
done < file.dat 2> /dev/null
}

Also, be more specific when you indicate that something doesn't work. Provide examples and what you have tried in order to correct the issue. We're not really here to do your work but just to guide you along the way.

Once again, i thank you all for your patience in helping me. At the time being, the code looks like this

#!/bin/bash

#later, i'll copy teste.gif to teste$TIMESTAMP.gif
TIMESTAMP=`date "+%d%m%Y%H%M"`

#I don't want to change the original file, so let's make a copy
cp map.gif mapa.gif

while read COLUNA LINHA
do
echo "COLUNA : $col"
echo "LINHA : $lin"
convert -fill red -draw 'text $col,$lin "*"' mapa.gif teste.gif
done < below

As you can see, the basic idea is to plot a kind of a map, with the help of imagemagik's convert. The basic idea is to populate a pre-existing map with *'s, read from the file "below" (Columns 1 and 2).

The output from the code is erroneous, since i get the following error message (for each line...):

COLUNA :
LINHA :
convert: Non-conforming drawing primitive definition `text'.

Could you have the kindness of commenting the code?
Thanks again:
Luis

to start with......

while read COLUNA LINHA
do
    echo "COLUNA : $COLUNA "
    echo "LINHA : $LINHA"
    convert -fill red -draw "text $COLUNA ,$LINHA '*'" mapa.gif teste.gif
done < below

Try this:

{
while read COLUMN LINE COUNT
do
    if [[ ${COLUMN} != +([0-9]) || ${LINE} != +([0-9]) ]]; then
       continue
    fi

    echo "COLUNA : ${COLUMN}"
    echo "LINHA : ${LINE}"
    convert -fill red -draw 'text ${COLUMN},${LINE} "*"' mapa.gif teste.gif 

done < file.dat 2> /dev/null
}

Whoops, I didn't really try to validate the convert command so you better look at vgersh99's example as well. The double quotes are necessary.

tmarikle,
you need double-quotes in order to have the shell expand the 'read-in' vars - COLUNA and LINHA.

I don't know much of the 'convert' myself......

Dear all:

the variable passing to convert is a little tricky, and i could solve the problem in convert with

convert -fill red -draw "text $LINHA,$COLUNA *" mapa.gif mapa.gif

As you've probably noticed, the code was wrong at the stage of invoking convert: i need to accumulate my data points...so the file always remains the same (mapa.gif).

The actual problem now resides on reading only the the pairs COLUNA, LINHA, on my table...since the script reads all the columns in the same line, as depicted on this simple output:

COLUNA : 1632
LINHA : 531 41.132 -8.397 52.000
convert: Non-conforming drawing primitive definition `text'.

For the time being, the code is:

#!/bin/bash

TIMESTAMP=`date "+%d%m%Y%H%M"`

cp map.gif mapa.gif

while read COLUNA LINHA
do
echo "COLUNA : $COLUNA"
echo "LINHA : $LINHA"
convert -fill red -draw "text $LINHA,$COLUNA *" mapa.gif mapa.gif
done < below

And the contents of file "below" is:
TOTAL DE PIXELS COM VALORES ABAIXO DO LIMIAR (9) : 261

COLUNA LINHA LATITUDE LONGITUDE COUNT :

1632    531    41.132    -8.397    52.000
1641    617    37.643    -7.611    52.000
1651    604    38.146    -7.311    52.000
1651    605    38.107    -7.307    52.000

Thank you all for your precious help and comments.

That's what I was attempting to say but maybe it wasn't clear. I only noticed the single quotes after your previous post. I was just partially being too lazy to edit the post and partially trying to prompt the OP to read your correction on that particular line.

That is because read needs to know how many things to read. If you specify while read COLUMN LINE JUNK then columns 3 through the end of the line will be dumped into read variable JUNK.