Starc Starc - 6 months ago 66
Bash Question

how to filter dbaccess output in informix?

I want to run dbaccess , and store the output to shell variable , I know there are two methods to do (i)output to pipe and (ii)unload to file. I want to use method (i) kind of approach to store query output to a shell variable but along with the query output I am getting unwanted things (connected to database , column headings ,disconnected ) image attached, I don't want to use method (ii) because I need to store query output to shell variable not a file. please help me with thisall the data which is going to shell variable I have attached

Answer

One way, not the best for some cases is sending stderr to /dev/null.

Let's create a table to test it:

[infx1210@tardis ~]$ dbaccess demo -

Database selected.

> CREATE TABLE starc (col1 INT, col2 INT);

Table created.

> INSERT INTO starc VALUES (1,1);

1 row(s) inserted.

> INSERT INTO starc VALUES (2,2);

1 row(s) inserted.

>

Database closed.

[infx1210@tardis ~]$

For one column and one row, ore more, this is quite enough:

[infx1210@tardis ~]$ out_1r1c=`echo "select col1 FROM starc WHERE col1 = 1" | dbaccess demo 2>/dev/null | uniq`
[infx1210@tardis ~]$ echo $out_1r1c
col1 1
[infx1210@tardis ~]$ out_2r1c=`echo "select col1 FROM starc" | dbaccess demo 2>/dev/null | uniq`
[infx1210@tardis ~]$ echo $out_2r1c
col1 1 2
[infx1210@tardis ~]$

For more than one column, probably not the best option:

[infx1210@tardis ~]$ out_1r2c=`echo "select * FROM starc WHERE col1 = 1" | dbaccess demo 2>/dev/null | uniq`
[infx1210@tardis ~]$ echo $out_1r2c
col1 col2 1 1
[infx1210@tardis ~]$ out_2r2c=`echo "select * FROM starc" | dbaccess demo 2>/dev/null | uniq`
[infx1210@tardis ~]$ echo $out_2r2c
col1 col2 1 1 2 2
[infx1210@tardis ~]$
Comments