Reading a table in a shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading a table in a shell script
# 1  
Old 08-10-2006
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
# 2  
Old 08-10-2006
One basic shell construct for doing this is:

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

# 3  
Old 08-10-2006
You can do something like this:
Code:
while read col1 col2
do
    echo "Col1 : $col1"
    echo "Col2 : $col2"
done < table.dat

Jean-Pierre.
# 4  
Old 08-10-2006
Again...the table

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
# 5  
Old 08-10-2006
and what exactly in the posted solutions did not work?
# 6  
Old 08-10-2006
Your text needs to have some known attributes such as a colon after your column headings for this to work:
Code:
{
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.
# 7  
Old 08-10-2006
Code..with your help

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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Help with pivoting table shell script

input file txt file 2000 1 name 2000 2 addr 2000 3 phone 2000 4 email 1000 1 name 1000 2 addr 1000 3 phone 1000 4 email 3000 1 name 3000 2 addr 3000 ... (4 Replies)
Discussion started by: senmatrix
4 Replies

2. Shell Programming and Scripting

Shell Script Table

Hi, i need a bit help. I must write a script with shell- and sed-commands, which reads a table from stdin and writes a html-table on stdout (so i can open it with a web browser). The number of columns must be a parameter for the script, so i can start it for example with: "./htmltab.sh 3... (3 Replies)
Discussion started by: scruffytramp
3 Replies

3. Shell Programming and Scripting

Reading a table from shell

i want to do something like this from shell, for any user without having to enter any passwords i want to store the value in the option_value column into a variable select option_value from `$databasename`.`wp_options` where option_name="template"; what is the command i should use? ... (0 Replies)
Discussion started by: vanessafan99
0 Replies

4. Shell Programming and Scripting

Perl Script for reading table format data from file.

Hi, i need a perl script which reads the file, content is given below. and output in new file. TARGET DRIVE IO1 IO2 IO3 IO4 IO5 ------------ --------- --------- --------- --------- --------- 0a.1.8 266 236 ... (3 Replies)
Discussion started by: asak
3 Replies

5. Shell Programming and Scripting

Shell script to query Oracle table

Hi, unix gurnis I need help for following requirement for writing a shell scritp. log in to oracle database, query one table total records (select count(*) from table1), pass the return value to a file. Thanks in advance (2 Replies)
Discussion started by: ken002
2 Replies

6. Shell Programming and Scripting

Create DB table through shell script

Hi, Can anyone tell me that, How to create table in Oracle database through shell script(ksh). Table contains 3 fields, 1] Emp ID, String, primary key 2] Name, String 3] B Date, date. Thanks in advance. (6 Replies)
Discussion started by: Poonamol
6 Replies

7. Shell Programming and Scripting

Truncating table from a shell script

I am trying to truncate a table using below script. When I ran the script it runs fine but table was not truncated and the spool is empty. I am not sure what is wrong with pl/sql block. #!/bin/ksh # ---------------------------------------------------------------------- # # Created by: XXXX... (2 Replies)
Discussion started by: gunaah
2 Replies

8. Shell Programming and Scripting

Script not reading Data from oracle table properly

Hi, I have a case where i am connecting to the oracle DB and reading a column value.... Script is in production...it was running fine,,,but suddenly now some times its started giving pain.... Script runs dailly....but sometimes its reading data from Oracle DB and sometimes its not rading the... (2 Replies)
Discussion started by: Sagarddd
2 Replies

9. Shell Programming and Scripting

How to connect DB2 table using shell script

Hi All, I want to connect two tables in DB2 using shell script and then compare the contents of two tables field by field. Could any one please help me in connecting database tables using Unix and retriving data from the same. Thanks, Durwas (0 Replies)
Discussion started by: dtidke
0 Replies

10. Shell Programming and Scripting

Alter Table Shell Script

I want to add some columns to a existing tables through a shell script. Please help. (2 Replies)
Discussion started by: ankitgupta
2 Replies
Login or Register to Ask a Question