Output alignment problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Output alignment problem
# 1  
Old 06-23-2010
Power Output alignment problem

Hi Gurus,

This is my raw data. How would I able to format this output into a better alignment?

Code:
/dev/vg00/lvol5 /home   0.12 GB 0.02 GB 0.10 GB 19%
/dev/vg00/misc  /misc   28.43 GB        4.92 GB 23.51 GB        17%
/dev/vg00/lvol6 /opt    8.25 GB 5.43 GB 2.82 GB 65%
/dev/vgsap/ora10264     /oracle/TEST/102_64      9.89 GB 8.37 GB 1.52 GB 84%
/dev/vgsap/mirra        /oracle/TEST/mirrlogA    0.48 GB 0.26 GB 0.21 GB 54%


Desired output:
Code:
/dev/vg00/lvol5      /home                    0.12 GB    0.02 GB    0.10 GB    19%
/dev/vg00/misc       /misc                    28.43 GB   4.92 GB    23.51 GB   17%
/dev/vg00/lvol6      /opt    		      8.25 GB    5.43 GB    2.82 GB    65%
/dev/vgsap/ora10264  /oracle/TEST/102_64      9.89 GB    8.37 GB    1.52 GB    84%
/dev/vgsap/mirra     /oracle/TEST/mirrlogA    0.48 GB    0.26 GB    0.21 GB    54%

I've tried to adopt the method that was posted in my previous article, but I'm running out luck to get the intended format.

Appreciate for any of your help and advice.

Thanks.


Regards,
Peter
# 2  
Old 06-23-2010
You will have to do it in 2 passes:
1st pass: calculate the maximum length for every field.
2nd pass: print the data with necessary padding based on length of every field.

Since some of the columns have embedded whitespace, you might have to put in some extra checks in calculating the value of every field.

Also, your sample data seems to be the output of 'df' command. On my Mac the output of df is aligned properly by default. May be there is an option on your system to do the same.

Code:
Filesystem                   512-blocks      Used  Available Capacity  Mounted on
/dev/disk0s2                  104857600  57935768   46409832    56%    /
devfs                               223       223          0   100%    /dev
/dev/disk0s3                  209715200 209123048     592152   100%    /Volumes/User

# 3  
Old 06-23-2010
Hi a_programmer,

Thanks for your response.

Yes, my raw data is from df command.

However, the data has been re-processed using a customized script to get the intended information.

Anybody can help with some scripting to produce the desired output?

Thanks a lot.


Regards,
Peter
# 4  
Old 06-23-2010
I know its ugly, but it works...
Code:
awk 'BEGIN{"awk '{$3=$3\" \"$4;$4=$5\" \"$6;$5=$7\" \"$8;$6=$9;
for (i=1;i<=NF-3;i++) if (max[i]<=length($i)) max[i]=length($i)}
END{for (i=1;i<=NF-3;i++) printf \"%s \",max[i]}' file"| getline;
for (i=1;i<=NF;i++) max[i]=$i}
{$3=$3" "$4;$4=$5" "$6;$5=$7" "$8;$6=$9;
for (i=1;i<=NF-3;i++) printf "%-"max[i]+2"s",$i;
printf "%s","\n"}' file

Put your filename where it is marked. You can also adjust number of spaces between columns by changing "+2" to something else.
This User Gave Thanks to bartus11 For This Post:
# 5  
Old 06-23-2010
Hi.

The best automatic, general-purpose alignment code I have seen is align: align | freshmeat.net

For example, on your code:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate alignment perl code, align.
# See: http://freshmeat.net/projects/align

# Infrastructure details, environment, commands for forum posts. 
# Uncomment export command to run script as external user.
# export PATH="/usr/local/bin:/usr/bin:/bin"
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
pe ; pe "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
pe "(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 printf specimen align
set -o nounset
pe

FILE=${1-data1}

# Display sample of data file, with head & tail as a last resort.
pe " || start [ first:middle:last ]"
specimen $FILE \
|| { pe "(head/tail)"; head -n 5 $FILE; pe " ||"; tail -n 5 $FILE; }
pe " || end"

pl " Results:"
align $FILE

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
printf - is a shell builtin [bash]
specimen (local) 1.17
align 1.7.0

 || start [ first:middle:last ]
Whole: 5:0:5 of 5 lines in file "data1"
/dev/vg00/lvol5 /home   0.12 GB 0.02 GB 0.10 GB 19%
/dev/vg00/misc  /misc   28.43 GB        4.92 GB 23.51 GB        17%
/dev/vg00/lvol6 /opt    8.25 GB 5.43 GB 2.82 GB 65%
/dev/vgsap/ora10264     /oracle/TEST/102_64      9.89 GB 8.37 GB 1.52 GB 84%
/dev/vgsap/mirra        /oracle/TEST/mirrlogA    0.48 GB 0.26 GB 0.21 GB 54%
 || end

-----
 Results:
/dev/vg00/lvol5     /home                  0.12 GB 0.02 GB  0.10 GB 19%
/dev/vg00/misc      /misc                 28.43 GB 4.92 GB 23.51 GB 17%
/dev/vg00/lvol6     /opt                   8.25 GB 5.43 GB  2.82 GB 65%
/dev/vgsap/ora10264 /oracle/TEST/102_64    9.89 GB 8.37 GB  1.52 GB 84%
/dev/vgsap/mirra    /oracle/TEST/mirrlogA  0.48 GB 0.26 GB  0.21 GB 54%

The OP seems to want a bit more room between the fields. There is a gutter setting that allows such repetition, but I have rarely used it.

If you are interested in using align, see the URL in the script listing.

Best wishes ... cheers, drl
# 6  
Old 06-23-2010
Hi bartus11,

Thanks for your response.

I've tried your script, but it was having some syntax issue. Had tried to fix it, but still unable to get it working.

Error:
Code:
 syntax error The source line is 1.
 The error context is
                BEGIN{"awk >>>  {=" <<<
 awk: The statement cannot be correctly parsed.
 The source line is 1.
        awk: There is a missing } character.
./test8.sh[8]: =":  not found.
./test8.sh[8]: =":  not found.
./test8.sh[8]: =:  not found.
./test8.sh[9]: Syntax error at line 9 : `(' is not expected.

Did I miss anything?

Please kindly advice.

Thanks.

Regards,
Peter

---------- Post updated at 10:47 AM ---------- Previous update was at 10:42 AM ----------

Hi drl,

Thanks for your response.

I've checked your script, it's too complicated for me to understand how it works, hence will be difficult for me to maintain it next time. Sorry.

However, I'm still unable to get it working using your script.

Any simpler script? Thanks.


Regards,
Peter
# 7  
Old 06-23-2010
Its not shell script. Put that code directly into command line. When you need new line use "\" before enter.

---

You can also put this
Code:
BEGIN{"awk '{$3=$3\" \"$4;$4=$5\" \"$6;$5=$7\" \"$8;$6=$9;
for (i=1;i<=NF-3;i++) if (max[i]<=length($i)) max[i]=length($i)}
END{for (i=1;i<=NF-3;i++) printf \"%s \",max[i]}' file"| getline;
for (i=1;i<=NF;i++) max[i]=$i}
{$3=$3" "$4;$4=$5" "$6;$5=$7" "$8;$6=$9;
for (i=1;i<=NF-3;i++) printf "%-"max[i]+2"s",$i;
printf "%s","\n"}

into file called for example script.awk, then call it like that:
Code:
awk -f script.awk file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Coloring cli output, screws up 'alignment'

Heyas I'm trying to add color 'support' to my TUI. It works, but behaves weird. Code in question: (status.conf) R="\033" ; TUI_WORK="" TUI_DONE="" ; TUI_FAIL="" TUI_SKIP="" ; TUI_NEXT="" TUI_BACK="" ; TUI_CANC="" TUI_ON="" ; TUI_OFF="" TUI_INFO="" ; TUI_HELP="" The... (4 Replies)
Discussion started by: sea
4 Replies

2. Shell Programming and Scripting

Text qualifier issue causing data alignment problem

Hello Everyone, I have a csv file with text qualifier as "" and data similar to below: "1","abc","address1","US" "2","def","address1 "characters in double-quote" address2","IND" "3","ghi","address1","UK" In above example, for record 2, we have an issue as in column3 contains double... (2 Replies)
Discussion started by: AnkitSenghani
2 Replies

3. Shell Programming and Scripting

Shell script output alignment

Hi How can I alaign the output of a script. Exaplme my script has assigned values to two variables, VAR1 and VAR2 VAR1=This is RAJ working as a DB2 UDB DBA for VISA Corporation, Need your help in Shell scripting VAR2= This is RAM working as a ORACLE DEVELOPER for TARGET... (1 Reply)
Discussion started by: thriloka
1 Replies

4. Shell Programming and Scripting

Formatting output alignment

Hi Gurus, I've the following output from my scripting as shown below. 0.48 GB 0.29 GB 0.19 GB 60% 0.48 GB 0.29 GB 0.19 GB 60% 228.90 GB 89.47 GB 139.42 GB 39% 228.76 GB 72.37 GB 156.39 GB 31% Is it possible to format this output into a proper... (16 Replies)
Discussion started by: superHonda123
16 Replies

5. Shell Programming and Scripting

alignment

Hi, I am having a file with with format. however, for longer xml, the xml code has been truncated like this. F1 |###################### |String1 |<XML><REQ><MSGTYPE>DBDIRECT</MSGTYPE><SYNC>0</SYNC><CLIENT>C11</CLIENT>NAME=MYNAME|JOB=MYJOB| | ... (3 Replies)
Discussion started by: shellwell
3 Replies

6. Shell Programming and Scripting

Text Alignment Problem

Dear Friends, I've one file "sample.log" with the below comma separated lines: BOND_FORWARD,0 succeeded,0 failed. EQUITY_FORWARD,0 succeeded,0 failed. FRA,12 succeeded,0 failed. OPTION_BAR,16 succeeded,0 failed. OPTION_VAN,76 succeeded,0 failed. RENTOPT_CAP_FLOOR,4775 succeeded,0 failed.... (2 Replies)
Discussion started by: ganapati
2 Replies

7. Programming

how to round up a memory address(memory alignment problem)

Hi, I try to marshal a unsigned int and a char * into a buffer, and then unmarshal them later to get them out. I need to put the char * in the front and unsigned int at the end of the buffer. However, my system always give me "BUS ERROR". I am using Sun Sparcs Sloris 2.10. My code to marshal... (6 Replies)
Discussion started by: nj302
6 Replies

8. Solaris

Memory Alignment Problem on Sun Sparcs

Hi, I try to marshal a unsigned int and a char * into a buffer, and then unmarshal them later to get them out. I need to put the char * in the front and unsigned int at the end of the buffer. However, my system always give me "BUS ERROR". I am using Sun Sparcs Sloris 2.10. My code to marshal the... (1 Reply)
Discussion started by: nj302
1 Replies

9. Shell Programming and Scripting

output display alignment !!

Hi I'm trying to display the output of my script in a friendly viewable format. it's something like this.. i have this while loop... in which i get some records from a file where fields are delimitered with a pipe. so i'm extacting each field and replacing the pipe with a \t, tab !!.. cat... (7 Replies)
Discussion started by: rosh0623
7 Replies

10. UNIX for Dummies Questions & Answers

ls command alignment problem

With older Linux servers, the command: ls -al would output text that would have the filenames all lined up in the same column (the owner and group names were truncated to produce a uniform right column). In newer distros (i.e. RHEL 3), the command will push the right column out when owners... (0 Replies)
Discussion started by: robf
0 Replies
Login or Register to Ask a Question