emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Creating hline in table from source block?
@ 2013-11-25 10:52 Loris Bennett
  2013-11-25 20:13 ` Achim Gratz
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Loris Bennett @ 2013-11-25 10:52 UTC (permalink / raw)
  To: emacs-orgmode

Dear List,

This

#+BEGIN_SRC sh
echo "a b c d"
echo "1 2 3 4"
echo "5 6 7 8"
#+END_SRC

produces this:

#+RESULTS:
|   a | b | c | d |
|   1 | 2 | 3 | 4 |
|   5 | 6 | 7 | 8 |

How do I get this

#+RESULTS:
|   a | b | c | d |
|-----+---+---+---|
|   1 | 2 | 3 | 4 |
|   5 | 6 | 7 | 8 |

?

Thanks,

Loris
-- 
This signature is currently under construction.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Creating hline in table from source block?
  2013-11-25 10:52 Creating hline in table from source block? Loris Bennett
@ 2013-11-25 20:13 ` Achim Gratz
  2013-11-25 20:43   ` Achim Gratz
  2013-11-25 21:38 ` Rasmus
  2013-11-26  2:41 ` Eric Schulte
  2 siblings, 1 reply; 12+ messages in thread
From: Achim Gratz @ 2013-11-25 20:13 UTC (permalink / raw)
  To: emacs-orgmode

Loris Bennett writes:
> How do I get this
>
> #+RESULTS:
> |   a | b | c | d |
> |-----+---+---+---|
> |   1 | 2 | 3 | 4 |
> |   5 | 6 | 7 | 8 |

You really don't want to use a shell for that, but if you insist, this
would do it:

--8<---------------cut here---------------start------------->8---
#+BEGIN_SRC sh :results raw
exec 3>&1 >&-
(
echo "a b c d"
echo ""
echo "1 2 3 4"
echo "5 6 7 8"
) | sed -re 's/^| /|/g' -e 's/^\|$/|-/' >&3
#+END_SRC
--8<---------------cut here---------------end--------------->8---


Regards,
Achim.
-- 
+<[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk Blofeld]>+

Samples for the Waldorf Blofeld:
http://Synth.Stromeko.net/Downloads.html#BlofeldSamplesExtra

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Creating hline in table from source block?
  2013-11-25 20:13 ` Achim Gratz
@ 2013-11-25 20:43   ` Achim Gratz
  2013-11-26 16:40     ` Daniel E. Doherty
  0 siblings, 1 reply; 12+ messages in thread
From: Achim Gratz @ 2013-11-25 20:43 UTC (permalink / raw)
  To: emacs-orgmode

Achim Gratz writes:
[…]

Looking at this again, of course this simpler version would do the same.

--8<---------------cut here---------------start------------->8---
#+BEGIN_SRC sh :results raw
(
echo "a b c d"
echo ""
echo "1 2 3 4"
echo "5 6 7 8"
) | sed -re 's/^| /|/g' -e 's/^\|$/|-/'
#+END_SRC
--8<---------------cut here---------------end--------------->8---


Regards,
Achim.
-- 
+<[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk Blofeld]>+

Factory and User Sound Singles for Waldorf Blofeld:
http://Synth.Stromeko.net/Downloads.html#WaldorfSounds

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Creating hline in table from source block?
  2013-11-25 10:52 Creating hline in table from source block? Loris Bennett
  2013-11-25 20:13 ` Achim Gratz
@ 2013-11-25 21:38 ` Rasmus
  2013-11-26  0:47   ` Rasmus
  2013-11-26  2:41 ` Eric Schulte
  2 siblings, 1 reply; 12+ messages in thread
From: Rasmus @ 2013-11-25 21:38 UTC (permalink / raw)
  To: emacs-orgmode

Hi Loris,

"Loris Bennett" <loris.bennett@fu-berlin.de> writes:

> How do I get this
>
> #+RESULTS:
> |   a | b | c | d |
> |-----+---+---+---|
> |   1 | 2 | 3 | 4 |
> |   5 | 6 | 7 | 8 |

You might find use of the post header argument here,

    http://orgmode.org/manual/post.html

In this case it's as simple as this

#+NAME: add_hline
#+BEGIN_SRC emacs-lisp :var data='((a b c)) h1=1
  (require 'cl)
  (append (subseq data 0 h1 ) (list 'hline) (subseq data h1))
#+END_SRC
#+BEGIN_SRC sh :post add_hline(data=*this*)
echo "a b c d"
echo "1 2 3 4"
echo "5 6 7 8"
#+END_SRC
 
Probably you can remove the require 'cl.  Also, you can generalize the
code to take arbitrarily hline positions.  

Hope it helps.

–Rasmus

-- 
Vote for proprietary math!

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Creating hline in table from source block?
  2013-11-25 21:38 ` Rasmus
@ 2013-11-26  0:47   ` Rasmus
  0 siblings, 0 replies; 12+ messages in thread
From: Rasmus @ 2013-11-26  0:47 UTC (permalink / raw)
  To: emacs-orgmode

Rasmus <rasmus@gmx.us> writes:

> Probably you can remove the require 'cl.  Also, you can generalize the
> code to take arbitrarily hline positions.  

Here's a more fun :post function (again depending on cl).

#+NAME: add_hline
#+BEGIN_SRC emacs-lisp :var data='((a b c) (d e f) (h i j)) h='(0 -2 100 last)
  (let* ((d (copy-list data))
         (h0 (delq nil
                   (mapcar (lambda (e)
                             (when (or (not (numberp e)) (< (abs e) (length data)))
                               (cond 
                                ((equal e 'last) (length data))
                                ((and (numberp e) (< e 0)) (+ (length data) e))
                                (t e))))
                           (if (listp h) h (list h)))))
         (h1 (number-sequence 0 (1- (length h0))))
         (H (mapcar* '+ h0 h1))) 
    (dolist (x H) (push 'hline (nthcdr x d)))
    d)
#+END_SRC

#+BEGIN_SRC sh :post add_hline(data=*this*, h='(0 1 last))
echo "a b c d"
echo "1 2 3 4"
echo "5 6 7 8"
#+END_SRC

#+RESULTS:
|---+---+---+---|
| a | b | c | d |
|---+---+---+---|
| 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 |
|---+---+---+---|


-- 
In theory, practice and theory are the same. In practice they are not

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Creating hline in table from source block?
  2013-11-25 10:52 Creating hline in table from source block? Loris Bennett
  2013-11-25 20:13 ` Achim Gratz
  2013-11-25 21:38 ` Rasmus
@ 2013-11-26  2:41 ` Eric Schulte
  2013-11-26  8:05   ` Loris Bennett
  2 siblings, 1 reply; 12+ messages in thread
From: Eric Schulte @ 2013-11-26  2:41 UTC (permalink / raw)
  To: Loris Bennett; +Cc: emacs-orgmode

"Loris Bennett" <loris.bennett@fu-berlin.de> writes:

> Dear List,
>
> This
>
> #+BEGIN_SRC sh
> echo "a b c d"
> echo "1 2 3 4"
> echo "5 6 7 8"
> #+END_SRC
>
> produces this:
>
> #+RESULTS:
> |   a | b | c | d |
> |   1 | 2 | 3 | 4 |
> |   5 | 6 | 7 | 8 |
>
> How do I get this
>
> #+RESULTS:
> |   a | b | c | d |
> |-----+---+---+---|
> |   1 | 2 | 3 | 4 |
> |   5 | 6 | 7 | 8 |
>
> ?
>

Most easily done with an Emacs Lisp code block.

#+BEGIN_SRC emacs-lisp
  '((a b c d)
    hline
    (1 2 3 4)
    (5 6 7 8))
#+END_SRC

#+RESULTS:
| a | b | c | d |
|---+---+---+---|
| 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 |

-- 
Eric Schulte
https://cs.unm.edu/~eschulte
PGP: 0x614CA05D

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Creating hline in table from source block?
  2013-11-26  2:41 ` Eric Schulte
@ 2013-11-26  8:05   ` Loris Bennett
  2013-11-26  8:54     ` Rasmus
  2013-11-26 13:14     ` Eric Schulte
  0 siblings, 2 replies; 12+ messages in thread
From: Loris Bennett @ 2013-11-26  8:05 UTC (permalink / raw)
  To: emacs-orgmode

Eric Schulte <schulte.eric@gmail.com> writes:

> "Loris Bennett" <loris.bennett@fu-berlin.de> writes:
>
>> Dear List,
>>
>> This
>>
>> #+BEGIN_SRC sh
>> echo "a b c d"
>> echo "1 2 3 4"
>> echo "5 6 7 8"
>> #+END_SRC
>>
>> produces this:
>>
>> #+RESULTS:
>> |   a | b | c | d |
>> |   1 | 2 | 3 | 4 |
>> |   5 | 6 | 7 | 8 |
>>
>> How do I get this
>>
>> #+RESULTS:
>> |   a | b | c | d |
>> |-----+---+---+---|
>> |   1 | 2 | 3 | 4 |
>> |   5 | 6 | 7 | 8 |
>>
>> ?
>>
>
> Most easily done with an Emacs Lisp code block.
>
> #+BEGIN_SRC emacs-lisp
>   '((a b c d)
>     hline
>     (1 2 3 4)
>     (5 6 7 8))
> #+END_SRC
>
> #+RESULTS:
> | a | b | c | d |
> |---+---+---+---|
> | 1 | 2 | 3 | 4 |
> | 5 | 6 | 7 | 8 |

Oops, my example was obviously a little too simple.  What I am actually doing
is something like this:

#+NAME: current_data
#+BEGIN_SRC sh :dir /home/loris/remote/far/far/away/results
echo "step level time clicks"
grep statistics *.log | awk '{print $(NF-9),"\t",$(NF-6),"\t",$(NF-4),"\t",$(NF-1)}'
#+END_SRC

So Achim's suggestion of using ":results raw" might be the way to go
(once I've worked out what the sed bit is doing ...)

However, the table will ultimately be around 40000 lines long,
so from a performance point of view it would be nice not to have to pipe
the whole thing through sed, particularly as the files are on a remote
server.  Having said that though, it is not a performance-critical
application.

So maybe I'll look at Rasmus' simpler suggestion (my lisp skill are
unfortunately not quite up to the "fun" variant ...)

How complex would the elisp version of my grep and awk above be?  Just
having one code block would be a little neater from my point of view.

Thanks for all the help.

Cheers,

Loris

-- 
This signature is currently under construction.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Creating hline in table from source block?
  2013-11-26  8:05   ` Loris Bennett
@ 2013-11-26  8:54     ` Rasmus
  2013-11-26 13:14     ` Eric Schulte
  1 sibling, 0 replies; 12+ messages in thread
From: Rasmus @ 2013-11-26  8:54 UTC (permalink / raw)
  To: emacs-orgmode

"Loris Bennett" <loris.bennett@fu-berlin.de> writes:

> So Achim's suggestion of using ":results raw" might be the way to go
> (once I've worked out what the sed bit is doing ...)
>
> However, the table will ultimately be around 40000 lines long,
> so from a performance point of view it would be nice not to have to pipe
> the whole thing through sed, particularly as the files are on a remote
> server.  Having said that though, it is not a performance-critical
> application.
>
> So maybe I'll look at Rasmus' simpler suggestion (my lisp skill are
> unfortunately not quite up to the "fun" variant ...)

The "fun" variant works with any table.  You just specify a list of
positions where you want hlines, e.g. '(0 1 last) or '(0 1 -1).

> How complex would the elisp version of my grep and awk above be?  Just
> having one code block would be a little neater from my point of view.

If you use :post you can keep your utility functions in a :noexport:
section at the bottom of your file (or even in a library-of-babel (see
org-babel-lob-ingest)) and you don't litter your code with unnecessary
stuff.  It's also usable across blocks.  

–Rasmus
 
-- 
When the facts change, I change my mind. What do you do, sir?

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Creating hline in table from source block?
  2013-11-26  8:05   ` Loris Bennett
  2013-11-26  8:54     ` Rasmus
@ 2013-11-26 13:14     ` Eric Schulte
  2013-11-26 14:23       ` Loris Bennett
  1 sibling, 1 reply; 12+ messages in thread
From: Eric Schulte @ 2013-11-26 13:14 UTC (permalink / raw)
  To: Loris Bennett; +Cc: emacs-orgmode

"Loris Bennett" <loris.bennett@fu-berlin.de> writes:

> Eric Schulte <schulte.eric@gmail.com> writes:
>
>> "Loris Bennett" <loris.bennett@fu-berlin.de> writes:
>>
>>> Dear List,
>>>
>>> This
>>>
>>> #+BEGIN_SRC sh
>>> echo "a b c d"
>>> echo "1 2 3 4"
>>> echo "5 6 7 8"
>>> #+END_SRC
>>>
>>> produces this:
>>>
>>> #+RESULTS:
>>> |   a | b | c | d |
>>> |   1 | 2 | 3 | 4 |
>>> |   5 | 6 | 7 | 8 |
>>>
>>> How do I get this
>>>
>>> #+RESULTS:
>>> |   a | b | c | d |
>>> |-----+---+---+---|
>>> |   1 | 2 | 3 | 4 |
>>> |   5 | 6 | 7 | 8 |
>>>
>>> ?
>>>
>>
>> Most easily done with an Emacs Lisp code block.
>>
>> #+BEGIN_SRC emacs-lisp
>>   '((a b c d)
>>     hline
>>     (1 2 3 4)
>>     (5 6 7 8))
>> #+END_SRC
>>
>> #+RESULTS:
>> | a | b | c | d |
>> |---+---+---+---|
>> | 1 | 2 | 3 | 4 |
>> | 5 | 6 | 7 | 8 |
>
> Oops, my example was obviously a little too simple.  What I am actually doing
> is something like this:
>
> #+NAME: current_data
> #+BEGIN_SRC sh :dir /home/loris/remote/far/far/away/results
> echo "step level time clicks"
> grep statistics *.log | awk '{print $(NF-9),"\t",$(NF-6),"\t",$(NF-4),"\t",$(NF-1)}'
> #+END_SRC
>

How about

#+NAME: current_data
#+BEGIN_SRC sh :dir /home/loris/remote/far/far/away/results :colnames '(step level time clicks)
grep statistics *.log | awk '{print $(NF-9),"\t",$(NF-6),"\t",$(NF-4),"\t",$(NF-1)}'
#+END_SRC

or do the column names need to be generated by the code block?

>
> So Achim's suggestion of using ":results raw" might be the way to go
> (once I've worked out what the sed bit is doing ...)
>
> However, the table will ultimately be around 40000 lines long,
> so from a performance point of view it would be nice not to have to pipe
> the whole thing through sed, particularly as the files are on a remote
> server.  Having said that though, it is not a performance-critical
> application.
>
> So maybe I'll look at Rasmus' simpler suggestion (my lisp skill are
> unfortunately not quite up to the "fun" variant ...)
>
> How complex would the elisp version of my grep and awk above be?  Just
> having one code block would be a little neater from my point of view.
>
> Thanks for all the help.
>
> Cheers,
>
> Loris

-- 
Eric Schulte
https://cs.unm.edu/~eschulte
PGP: 0x614CA05D

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Creating hline in table from source block?
  2013-11-26 13:14     ` Eric Schulte
@ 2013-11-26 14:23       ` Loris Bennett
  2013-11-26 20:07         ` Achim Gratz
  0 siblings, 1 reply; 12+ messages in thread
From: Loris Bennett @ 2013-11-26 14:23 UTC (permalink / raw)
  To: emacs-orgmode

Eric Schulte <schulte.eric@gmail.com> writes:

> "Loris Bennett" <loris.bennett@fu-berlin.de> writes:
>
>> Eric Schulte <schulte.eric@gmail.com> writes:
>>
>>> "Loris Bennett" <loris.bennett@fu-berlin.de> writes:
>>>
>>>> Dear List,
>>>>
>>>> This
>>>>
>>>> #+BEGIN_SRC sh
>>>> echo "a b c d"
>>>> echo "1 2 3 4"
>>>> echo "5 6 7 8"
>>>> #+END_SRC
>>>>
>>>> produces this:
>>>>
>>>> #+RESULTS:
>>>> |   a | b | c | d |
>>>> |   1 | 2 | 3 | 4 |
>>>> |   5 | 6 | 7 | 8 |
>>>>
>>>> How do I get this
>>>>
>>>> #+RESULTS:
>>>> |   a | b | c | d |
>>>> |-----+---+---+---|
>>>> |   1 | 2 | 3 | 4 |
>>>> |   5 | 6 | 7 | 8 |
>>>>
>>>> ?
>>>>
>>>
>>> Most easily done with an Emacs Lisp code block.
>>>
>>> #+BEGIN_SRC emacs-lisp
>>>   '((a b c d)
>>>     hline
>>>     (1 2 3 4)
>>>     (5 6 7 8))
>>> #+END_SRC
>>>
>>> #+RESULTS:
>>> | a | b | c | d |
>>> |---+---+---+---|
>>> | 1 | 2 | 3 | 4 |
>>> | 5 | 6 | 7 | 8 |
>>
>> Oops, my example was obviously a little too simple.  What I am actually doing
>> is something like this:
>>
>> #+NAME: current_data
>> #+BEGIN_SRC sh :dir /home/loris/remote/far/far/away/results
>> echo "step level time clicks"
>> grep statistics *.log | awk '{print $(NF-9),"\t",$(NF-6),"\t",$(NF-4),"\t",$(NF-1)}'
>> #+END_SRC
>>
>
> How about
>
> #+NAME: current_data
> #+BEGIN_SRC sh :dir /home/loris/remote/far/far/away/results :colnames '(step level time clicks)
> grep statistics *.log | awk '{print $(NF-9),"\t",$(NF-6),"\t",$(NF-4),"\t",$(NF-1)}'
> #+END_SRC
>
> or do the column names need to be generated by the code block?

No, the column names are fixed, so that's perfect, thank you.  I
suspected there might be some more straight-forward way than the
interesting, but slightly more involved methods suggested by Achim and
Rasmus.

However, it is good have some examples for ":results raw" and ":post"
and I shall certainly be looking into "Library of Babel", as I seem to
be sliding gradually into programming in Org.

Cheers,

Loris

>> So Achim's suggestion of using ":results raw" might be the way to go
>> (once I've worked out what the sed bit is doing ...)
>>
>> However, the table will ultimately be around 40000 lines long,
>> so from a performance point of view it would be nice not to have to pipe
>> the whole thing through sed, particularly as the files are on a remote
>> server.  Having said that though, it is not a performance-critical
>> application.
>>
>> So maybe I'll look at Rasmus' simpler suggestion (my lisp skill are
>> unfortunately not quite up to the "fun" variant ...)
>>
>> How complex would the elisp version of my grep and awk above be?  Just
>> having one code block would be a little neater from my point of view.
>>
>> Thanks for all the help.
>>
>> Cheers,
>>
>> Loris

-- 
Dr. Loris Bennett (Mr.)
ZEDAT, Freie Universität Berlin         Email loris.bennett@fu-berlin.de

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Creating hline in table from source block?
  2013-11-25 20:43   ` Achim Gratz
@ 2013-11-26 16:40     ` Daniel E. Doherty
  0 siblings, 0 replies; 12+ messages in thread
From: Daniel E. Doherty @ 2013-11-26 16:40 UTC (permalink / raw)
  To: Achim Gratz; +Cc: emacs-orgmode


And if you are generating this programatically, this works:

#+BEGIN_SRC sh :results raw
(
echo "|a |b |c |d"
echo "|-"
echo "|1 |2 |3 |4"
echo "|5 |6 |7 |8"
)
#+END_SRC


-- 
====================================================
Daniel E. Doherty

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Creating hline in table from source block?
  2013-11-26 14:23       ` Loris Bennett
@ 2013-11-26 20:07         ` Achim Gratz
  0 siblings, 0 replies; 12+ messages in thread
From: Achim Gratz @ 2013-11-26 20:07 UTC (permalink / raw)
  To: emacs-orgmode

Loris Bennett writes:
> No, the column names are fixed, so that's perfect, thank you.  I
> suspected there might be some more straight-forward way than the
> interesting, but slightly more involved methods suggested by Achim and
> Rasmus.

There might be way to do that, but it's not implemented for shell blocks
since there isn't an easy way to deal with arrays in a shell.  You'd be
much better off using a language that has support for that (like Perl).

In your case (when the first line is always a header and you don't need
hlines anywhere else), the following trick using the end header
arguments of a call line works just as well:

--8<---------------cut here---------------start------------->8---
#+NAME: table
#+BEGIN_SRC sh :results table
echo "a b c d"
echo "1 2 3 4"
echo "5 6 7 8"
#+END_SRC

#+CALL: table() :results table :colnames yes

#+RESULTS:
| a | b | c | d |
|---+---+---+---|
| 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 |
--8<---------------cut here---------------end--------------->8---


Regards,
Achim.
-- 
+<[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk Blofeld]>+

SD adaptations for KORG EX-800 and Poly-800MkII V0.9:
http://Synth.Stromeko.net/Downloads.html#KorgSDada

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2013-11-26 20:08 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-25 10:52 Creating hline in table from source block? Loris Bennett
2013-11-25 20:13 ` Achim Gratz
2013-11-25 20:43   ` Achim Gratz
2013-11-26 16:40     ` Daniel E. Doherty
2013-11-25 21:38 ` Rasmus
2013-11-26  0:47   ` Rasmus
2013-11-26  2:41 ` Eric Schulte
2013-11-26  8:05   ` Loris Bennett
2013-11-26  8:54     ` Rasmus
2013-11-26 13:14     ` Eric Schulte
2013-11-26 14:23       ` Loris Bennett
2013-11-26 20:07         ` Achim Gratz

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).