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.
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
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
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!
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
"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
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.
"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?
"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
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
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
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