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
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)
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:
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
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:
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.