|
Programming C, bash, Python, Perl, PHP, Java, you name it. |
|
Thread Tools | Display Modes |
|
|
|||
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 Code:
column1 column2 column3 column4 1 2 a b c d e f 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? |
|
|||
Quote:
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. |
|
|||
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. |
|
|||
Quote:
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... ). |
|
|||
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 |
|
|||
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? |
|
|||
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. |
|
|||
In the awk BEGIN block you can initialize the special variable FS (Field Separator) as "&"
Code:
FS = "&"
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
|||
no, I'm not understanding, I guess. The values are not declared, are they?
Poor me |
|
|||
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 |
|
||||
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''. |
|
|
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 |