DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 19th March 2009
gosha gosha is offline
Spam Deminer
 
Join Date: Jun 2008
Location: China
Posts: 256
Thanked 0 Times in 0 Posts
Default table formatting (could not find better title)

I have a table with the following sample values:
Code:
column1     column2     column3     column4
1           2           a           b
1           2           c           d
1           2           e           f
is there a way to get this:
Code:
column1     column2     column3     column4
1           2           a           b
                        c           d
                        e           f
I have more columns, but it is only to explain.
In fact this table is an output of postgresql, but I understand that this formatting is not possible in sql (right?). Of course, I could connect with MsAccess and use its report wizard, but I'd like to learn to do without Windows alltogether. If I had a i386 machine, I could try using OpenOffice. I checked google and found tools like Gedafe and Papyrus, but cannot get Gedafe to work (I know, I have to study) and Papyrus seems to be 100% Linux-centric.
So, my question is: can I do it with a tool like sed? Or how could I do it? Working with the computer, I very often get into this kind of formatting problems. What would the best language to learn to solve problems in autonomy?
Reply With Quote
  #2   (View Single Post)  
Old 19th March 2009
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 2,888
Thanked 190 Times in 160 Posts
Default

Quote:
Originally Posted by gosha View Post
I have a table with the following sample values:
Code:
column1     column2     column3     column4
1           2           a           b
1           2           c           d
1           2           e           f
is there a way to get this:
Code:
column1     column2     column3     column4
1           2           a           b
                        c           d
                        e           f
It is unclear why the first two columns change, but the other two remain the same from the first representation to the second. If this is simply an oversight, yes, sed(1) can be used to reformat it, but personally, I would find this akin to extracting my own teeth with a pair of pliers.

With whatever database you are using, you should have some form of administrative console which allows you to interact with the database by typing in SQL commands. The output of SELECT statements may be formatted well enough for your needs. If you need more control, you should be able to capture the output of the administrative console, & manipulate the output with awk(1), perl(1), or a host of other scripting tools.

If logic is required to move from the first representation above to the second, I would highly recommend awk, perl, et. al.. sed will not be able to perform sophisticated logic.

In particular, if you are wanting to format columns, look at the printf() function available in any of the scripting languages (even in Python...) as this will allow you to fix the column size even when the text in the column varies.
Reply With Quote
  #3   (View Single Post)  
Old 19th March 2009
gosha gosha is offline
Spam Deminer
 
Join Date: Jun 2008
Location: China
Posts: 256
Thanked 0 Times in 0 Posts
Default

Well, let me tell you the whole story.
This is a small dictionary for research purposes, and I want to take away the redundant "1" and "2" simply because it becomes more readable (at least for me).
What I do now is use the "\f" format command in psql to put a "&" as a column separator, then I output the query to a file and print it out with latex. So I guess this extra formatting operation I want would be done on the output file with the "&" as column separators.
Reply With Quote
  #4   (View Single Post)  
Old 19th March 2009
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 3,154
Thanked 182 Times in 149 Posts
Default

Something like this?
Code:
$ cat test-case.txt

1       2       a       b
1       2       c       d
1       2       e       f
1       3       a       b
1       3       c       d

$ cat gosha-awk                                
#!/usr/bin/awk -f

BEGIN {
col_1_prev = ""
col_2_prev = ""
}

{
if ( $1 == col_1_prev  &&  $2 == col_2_prev )  
  printf "\t\t%s\t%s\n", $3, $4  
else
  printf "%s\t%s\t%s\t%s\n", $1, $2, $3, $4 

col_1_prev = $1
col_2_prev = $2
}

$ ./gosha-awk test-case.txt  
1       2       a       b
                c       d
                e       f
1       3       a       b
                c       d
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #5   (View Single Post)  
Old 19th March 2009
gosha gosha is offline
Spam Deminer
 
Join Date: Jun 2008
Location: China
Posts: 256
Thanked 0 Times in 0 Posts
Default

Great! It is exactly what I need!
But one question: what if the output is already formatted with the "&" as column separators? In that case if you cat the file they will be displayed as a continuous line (I will do the tabular in latex). how do I tell awk to use "&" as column separators?
Reply With Quote
  #6   (View Single Post)  
Old 19th March 2009
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 2,888
Thanked 190 Times in 160 Posts
Default

Quote:
Originally Posted by gosha View Post
...I want to take away the redundant "1" and "2" simply because it becomes more readable (at least for me).
Understandable, but depending upon how far you want to remove redundancy, you may be needing to buffer many rows (at least the number of columns you are outputting...), & the logic will grow with the sophistication. Nevertheless, that is a decision you will need to make. It all depends upon whether the result is worth your time & effort.

Yet going back to your original question, I don't see that you will be able to do this kind of formatting in sed(1). The point being is that you are on a slippery slope of formatting wants & desires which will ultimately force you to resort to awk(1), perl(1), etc. soon, so you might as well be finding something more powerful than sed(1) now.

If you are reticent to move to a different tool due to unfamiliarity, google for "xyz tutorial", & you should be able to find enough to get started. This is particularly true of awk, & as I recall, there are some reasonable tutorials over at IBM's DeveloperWorks site (where ironically, there are tutorials for sed as well... ).
Reply With Quote
  #7   (View Single Post)  
Old 19th March 2009
gosha gosha is offline
Spam Deminer
 
Join Date: Jun 2008
Location: China
Posts: 256
Thanked 0 Times in 0 Posts
Default

No, sorry, I have to correct, this is not "exactly" what I need, but something very similar.
I need something more esotheric, that is I don't want to declare the values in the first two columns. The script should judge if the following rows have the same values and keep only the first one.
Maybe I'm asking too much.
Reply With Quote
  #8   (View Single Post)  
Old 19th March 2009
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 3,154
Thanked 182 Times in 149 Posts
Default

In the awk BEGIN block you can initialize the special variable FS (Field Separator) as "&"
Code:
FS = "&"
awk will now split the fields on the "&"
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #9   (View Single Post)  
Old 19th March 2009
gosha gosha is offline
Spam Deminer
 
Join Date: Jun 2008
Location: China
Posts: 256
Thanked 0 Times in 0 Posts
Default

no, I'm not understanding, I guess. The values are not declared, are they?
Poor me
Reply With Quote
Old 19th March 2009
gosha gosha is offline
Spam Deminer
 
Join Date: Jun 2008
Location: China
Posts: 256
Thanked 0 Times in 0 Posts
Default

ok, sorry for my usual babbling.
It is EXACTLY what I need!
Thanks a lot j65nko!
And thank you ocicat, I'll go for some awk and perl learning (time permitting...).

I just love OpenBSD
Reply With Quote
Old 19th March 2009
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

You can find a decent AWK tutorial here and manual here; Perl stuff is almost anywhere and everywhere on the web.

The books "Learning Perl" and the "Perl Cookbook" would be good buys if you choose to learn the language (it even has a special "format" feature to help pretty print garbage IAW with a report format).
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
I think I just mangled my partition table Mantazz FreeBSD Installation and Upgrading 2 2nd July 2009 09:55 PM
Formatting Hard Disk Drive to UFS in OS X 10.5 Turquoise88 Other BSD and UNIX/UNIX-like 1 7th March 2009 09:57 PM
Fluxbox + Firefox = UTF8 title problem.. DNAeon FreeBSD Ports and Packages 7 10th December 2008 08:52 AM
Formatting fat32 drive quickly map7 FreeBSD General 10 26th July 2008 05:17 PM
Can i change the title on my topic? aleunix Feedback and Suggestions 2 2nd June 2008 04:52 AM


All times are GMT. The time now is 04:15 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick