Want to format output.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Want to format output.
# 15  
Old 05-28-2010
Thanks Frans.....!!!

Superb.......jus one more help...... i jus want them to be formatted so it looks neat......as table name and numbers are of different length so all is getting jumbled...!

Code:
table 897     897     490
table1        8642    8642    7127
table1111        888     888     399
table2      50      0
table3      25597   25597   1691
table four       7798    7798    1691
table five       461     461     3513
table6      29      29      152
some_more_table7  30522   30522   1691
yet ANother_last TAble      462     462     3513

Hope will not have to bug you all after this....Smilie
Even if will have to, will come back and ask... Smilie
# 16  
Old 05-28-2010
I already thought so about the formatting Smilie
I wouldn't take care of the format before being sure having the right data.
I can't answer immediately (i'm a bit overbooked) but i'm sure i'll find a nice solution. Just say me the width and alignment you want for each column.
# 17  
Old 05-28-2010
What's this table for, a process or a human? If you want it to be machine processable, consistent formatting is much more important than pretty formatting.
# 18  
Old 05-28-2010
Pretty human-readable format (can be adjusted)
Code:
FORM='%-20s%8s%8s%8s\n'
split -l 10  Table_data.txt
IFS=',' # use another delimiter if ';' dosn'fit your needs
printf "$FORM" "TABLE NAME" "COUNT 1" "COUNT 2" "COUNT 3"
echo
paste -d "$IFS" Table_Name.txt xaa xab xac | while read A B C D
do    printf "$FORM" "$A" $B $C $D
done

# 19  
Old 05-29-2010
Hi.

The best automatic column alignment tool I have seen is align. It can found at align | freshmeat.net
An example of the use can be seen at the end of this script, using the frans split and paste method for this dataset:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate split, paste, align.
# See: http://freshmeat.net/projects/align

# Uncomment to run script as external user.
# export PATH="/usr/local/bin:/usr/bin:/bin"
# Infrastructure details, environment, commands for forum posts. 
set +o nounset
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo ; echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
c=$( ps | grep $$ | awk '{print $NF}' )
version >/dev/null 2>&1 && s=$(_eat $0 $1) || s=""
[ "$c" = "$s" ] && p="$s" || p="$c"
version >/dev/null 2>&1 && version "=o" $p specimen split paste align
set -o nounset

FILE1=${1-data1}
shift
FILE2=${1-data2}

# Sample data files with head / tail if specimen fails.
echo
specimen $FILE1 $FILE2 \
|| { echo "(head/tail)"; head -n 5 $FILE1 $FILE2; echo " ||" ;\
     tail -n 5 $FILE1 $FILE2; }

# Remove debris from previous runs.
rm xa?

echo
echo " Results:"
# split -l 10  Table_data.txt
# paste Table_Name.txt xaa xab xac
# paste data1 xaa xab xac |
split -l 10 data2
paste data1 xac xab xaa |
align -st

exit 0

Producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
specimen (local) 1.17
split (GNU coreutils) 6.10
paste (GNU coreutils) 6.10
align 1.7.0

Whole: 5:0:5 of 10 lines in file "data1"
table
table1
table1111
table2
table3
table four
table five
table6
some_more_table7
yet ANother_last TAble

Edges: 5:0:5 of 30 lines in file "data2"
490
7127
399
0
1691
   ---
7798
461
29
30522
462

 Results:
table			 897   897   490
table1			8642  8642  7127
table1111		 888   888   399
table2			  50	50     0
table3		       25597 25597  1691
table four		7798  7798  1691
table five		 461   461  3513
table6			  29	29   152
some_more_table7       30522 30522  1691
yet ANother_last TAble	 462   462  3513

As you can see it recognizes fields as alphabetic or numeric, and, with the separator option, will split fields by TABs. I adjusted the settings of paste to correspond to the output that the OP last displayed.

Adding column headings, if desired, is left as an exercise.

Best wishes ... cheers, drl

Note 1: the visual alignment of the output looks off in the second-last line compared to my terminal. The default join scheme is a combination of TAB and SPACE characters, so it looks to me as if the forum CODE block SPACE characters are somehow different in size from my terminal. I looked at the raw output and there are TABs between the alpha field and the first numeric column, but SPACES (as they should be) between the numeric columns. I'll continue to look at it, but it appears OK on my display. This is the first that I have noticed such an anomaly.

Note 2: The second last line does not include a TAB, only SPACES after the label, so I think that that is the source on the visual issue.

Note 3: Shortening the label in question produces:
Code:
table			 897   897   490
table1			8642  8642  7127
table1111		 888   888   399
table2			  50	50     0
table3		       25597 25597  1691
table four		7798  7798  1691
table five		 461   461  3513
table6			  29	29   152
ome_more_table7	       30522 30522  1691
yet ANother_last TAble	 462   462  3513

for which align then adds a TAB in that line, and columns appear aligned here. In both cases the output is visually aligned at my work display. This leads me to believe that SPACEs have a different value in CODE blocks compared to my workstation environment. I haven't used anything else other than Debian to look at this thread, so I suppose it is possible that the software chain is distorting the image for me, and that it appears OK to others. Perhaps I'll try looking at it with a Mac and a browser different from Firefox ( or Iceweasel, as it is known in Debian-land Smilie )

Note 4: From Mac OS X, Safari, both displays look fine, so the problem is on my end, somewhere in the browser or X. I may look at it more, but the issue is not critical for me.

Note 5: Back on Linux, browser Galeon. Both displays are way off.

Note 6: Linux, epiphany-browser. First display off, second OK. Seems to point to X and / or fonts.

Last edited by drl; 05-29-2010 at 01:18 PM..
# 20  
Old 06-01-2010
MySQL

Thanks a lot Frans....!!!
Its working fine now......would be incorporating it into my script and hope all goes fine.... Smilie

Thanks to others too...all of you made my work easy.....!!!


Will keep bugging you all....!!!Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Format output

Hello Team, I have the following details in a txt file (please note the spaces and tabs) C1 C2 C3 ------------------ --------------- ------------- abc, xyz 2 8 pqr 2 ... (9 Replies)
Discussion started by: H squared
9 Replies

2. Shell Programming and Scripting

Format with output

I have written some scripts that resulted in the table below (Column1 is ITEM, Column2 is Group, Column3 is Category and Column4 is Quantity) but I want the output in another format: Input: K123 X CATA 3 K123 Y CATA 4 K123 Z CATA 2 K123 X CATB 5 K123 Y CATB 2 K123 Z CATB 2 B65 M CATB... (7 Replies)
Discussion started by: aydj
7 Replies

3. 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

4. UNIX for Dummies Questions & Answers

Format Output

Hello Learned People, Good evening. I have absolutely no idea as how to do this & I admit that I do not know anything in unix. I must learn this one of these days. Im a help desk incharge today & someone gave me this file to be formatted & I have a file like this. vi NewFile.txt 232... (8 Replies)
Discussion started by: RTAN
8 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. UNIX for Advanced & Expert Users

format df -k output

i am running df -k command on aix machine. i got the output like this. i need to store into file and send that file into microsoft excel.i need to allign properly. Filesystem 512 blocks Free % Used Iused %Iused Mounted on /dev/hd4 262144 126488 52% ... (9 Replies)
Discussion started by: wintercoat
9 Replies

7. Shell Programming and Scripting

Help in Getting specified output format

Hi all, I have input text file of this format objectclass:endeavor pid:12345 postalAddress:379 PROSPECT ST street:STE B l:TORRINGTON st:CT postalCode:067905238 telephoneNumber:9999999999... (2 Replies)
Discussion started by: pintoo
2 Replies

8. Shell Programming and Scripting

How to format output

Dear all, I have written a program which access database and displays the values returned by the query . There are 10 columns to be displayed in one row. But am ending with 5 lines displayed on 1st line and the next 5 in the 2nd line. Ex : 21608 10-20-2007 148 Al's Appliance... (7 Replies)
Discussion started by: uday542
7 Replies

9. 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

10. UNIX for Dummies Questions & Answers

ls output format

below is the output is ls -l -rw-r--r-- 1 tonyt staff 3212 Apr 17 1999 file1 -rw-r--r-- 1 tonyt staff 4541 Mar 3 21:08 file2 why the date format is not the same? (6 Replies)
Discussion started by: tonyt
6 Replies
Login or Register to Ask a Question