How to format database output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to format database output
# 1  
Old 03-04-2008
How to format database output

I have an output from a database query on the unix standard output as under:

abc
dsagh
dsflsfds
dsfsdfsdfsd
sdf
sdfsd
sdfsd
sdfsdf
sdfsdfs
sdfsdf
sdfsdf
sdfsdf
sdfsdf
sdfsdf
sdfsdff
dsfsdf
sdfdf
sdf
dsf
sdfsdf
sdfsdf

I want to display it like this:

abc dsagh dsflsfds dsfsdfsdfsd sdf sdfsd
sdfsd sdfsdf sdfsdfs sdfsdf sdfsdf sdfsdf
sdfsdf sdfsdf sdfsdff dsfsdf sdfdf sdf
dsf sdfsdf sdfsdf

Can somebody help me in getting the required script at place?

Many thanks in advance.
# 2  
Old 03-04-2008
Code:
awk 'BEGIN {count=0}
       {
          printf("%s ", $0);
          count++;
          if(count % 6 ==0) {printf("\n")}
       }
       END {printf("\n") }
       ' inputfile > outputfile

Most db's support something like Oracle's spool command which directs the output into a file.
Otherwise you wil have to pipe the output into awk, if that is possible with your db query language.
What db are you using?
# 3  
Old 03-04-2008
Code:
nawk 'ORS=FNR%6?OFS:RS' myFile

# 4  
Old 03-04-2008
Jim,

The script did work out and the output is somewhat coming in columnar format but as the length of the values varies it does move the next column.

Is it possible to get this output in the form that "SELECT i in `sql st`"
displays. I mean it's exactly in the tabular format even if the length of the value varies.

For your info. I am using Netezza DB on Linux.
-----------------------------------------
vgersh99

Script dint workout.
It gave the following error:
0403-057 Syntax error at line 92 : `'' is not matched.
# 5  
Old 03-04-2008
please, post exactly what you tried using [code] and [/code] vB Code tags - more on the vB Code tags here.
# 6  
Old 03-04-2008
You did not specify columns to start with. the %10s makes columns ten characters wide. change it as you need it.
Code:
awk 'BEGIN {count=0}
       {
          printf("%s ", sprintf("%10s ", $0));
          count++;
          if(count % 6 ==0) {printf("\n")}
       }
       END {printf("\n") }
       ' inputfile > outputfile

# 7  
Old 03-04-2008
actually for a given user, I would get the list of groups and database names as the output (which I want to format) and then based on this give some options to the user whether they want to do with the user associated with a group or database.

The problem I am facing is the output scrolls to multiple pages and script user reallyhave to scroll up for the details which is bit annoying.

That's why I just want the data to be formatted in tabular or columnar format.

I don't know whether it's possible or not but it will be great help....

Here is the line in the script that is giving me the output and that I want to format:

echo "select grpname||"->"||databasnam from tablename where user="$USER"|nzsql <dbname>
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Connect to database with shell output

Dear All, I am trying to write a script which will generate output to a file based on input files on basis of a pattern which I am able to do so. But, I am stuck as I am trying to connect this to database server. Below is my command and output details. Input Code filedate=$(date... (6 Replies)
Discussion started by: grvk101
6 Replies

2. Shell Programming and Scripting

Script to generate Excel file or to SQL output data to Excel format/tabular format

Hi , i am generating some data by firing sql query with connecting to the database by my solaris box. The below one should be the header line of my excel ,here its coming in separate row. TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM... (6 Replies)
Discussion started by: dani1234
6 Replies

3. Programming

How to Format database output (DBMS_OUTPUT.PUT_LINE) in unix?

Dear All, As I'm new here, please forgive me if any rule violation occurred. I have a script like this: #! /bin/ksh # Author : Saptarshi # Date : 18-Mar-2011 # Version : 1.0 Return_op=`sqlplus -s <<ENDOFSQL db_user/db_pass@db_nm WHENEVER SQLERROR EXIT 1 set ... (1 Reply)
Discussion started by: saps19
1 Replies

4. Shell Programming and Scripting

Database password appearing in script output

Hi there, This is my first post, so as you have probably guessed I am looking for some help. Currently we have close to 1000 ksh scripts operating on HPUX servers that call either isql or bcp to connect to Sybase databases. Problem being that the db passwords are appearing in the job log... (3 Replies)
Discussion started by: kdk_irl
3 Replies

5. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

6. Shell Programming and Scripting

how to extract the data from database (oracle) and send the output as an .xls file?

Hi, How to extract the data from Oracle database and sent the output data to mails using mailx command with .xls attachement? Here i know how to connect the database using unix shell script and how to use the mailx command in UNIX script But i don't know how to use the .xls format file (i... (1 Reply)
Discussion started by: psiva_arul
1 Replies

7. Shell Programming and Scripting

capturing output from top and format output

Hi all, I'd like to capture the output from the 'top' command to monitor my CPU and Mem utilisation.Currently my command isecho date `top -b -n1 | grep -e Cpu -e Mem` I get the output in 3 separate lines.Tue Feb 24 15:00:03 Cpu(s): 3.4% us, 8.5% sy .. .. Mem: 1011480k total, 226928k used, ....... (4 Replies)
Discussion started by: new2ss
4 Replies

8. Shell Programming and Scripting

To spool output from a database query

Hi all, I would want to spool file for a database query, however by using crontab, the file is not spooled. Below shows my script: ORACLE_HOME="/u01/oraprod/perpdb/10.1.0/db_1" OUTFILE="/tmp/invalid.out" FILE="$HOME/admin/scripts" $ORACLE_HOME/bin/sqlplus -s "/as sysdba"... (0 Replies)
Discussion started by: *Jess*
0 Replies

9. UNIX for Dummies Questions & Answers

Output Multiple Field from dataBase file

I am fairly new in unix I was wondering if anybody can help me out with this: I am trying to output to a file the following fields; Field1 Field2 Field4 From a database file dataBase1. this is how the file looks: dataBase1 TABLE DATA Example ================== Table ... (3 Replies)
Discussion started by: Dennz
3 Replies
Login or Register to Ask a Question