Discussion:
[Orgmode] Passing a table to org-babel shell script
Matthias Teege
2010-01-24 09:47:34 UTC
Permalink
Hello,

I try to use a org-mode table as input to an shell script. It works, if
I use a Table with one column.

#+tblname: sec
| Hello World |

#+begin_src sh :var table=sec
cat <<EOF
$table
EOF
#+end_src

#+results:
: [[Hello World]]

But if I use more then one column, If got an error:

#+tblname: sec
| Hello | World |

#+begin_src sh :var table=sec
cat <<EOF
$table
EOF
#+end_src

#+results:
| sh: Zeile 1: World]]: command not found. |

It looks like, then Shell interprets the seperator "|" as pipe.

How do I use a multicolumn table as input for a shell script?

Many thanks
Matthias
Eric Schulte
2010-02-06 18:33:16 UTC
Permalink
Hi Matthias,

Sorry about the slow reply. This is a good question, I have comments
in-line below.

Matthias Teege <matthias-***@mteege.de> writes:

[...]
Post by Matthias Teege
#+tblname: sec
| Hello | World |
#+begin_src sh :var table=sec
cat <<EOF
$table
EOF
#+end_src
| sh: Zeile 1: World]]: command not found. |
It looks like, then Shell interprets the seperator "|" as pipe.
How do I use a multicolumn table as input for a shell script?
This is a good question, clearly the current approach of dumping it in
as a string isn't working, however I'm not sure what a good alternative
would be. Some ideas that come to mind are...

1) allowing the user to specify a separator with a header argument as
follows
#+begin_src sh :var table=sec :separator ,
cat <<EOF
$table
EOF
#+end_src

which would result in something like

: "Hello, World"

2) writing the table to a tab or comma separated file and then
replacing =$table= in the source block body with the path to the
file name, s.t. something like

#+begin_src sh :var table=data
wc $table
#+end_src

would return reasonable results

I guess this would largely depend on what types of values you are
passing to the shell, and what your use-case is.

I'd be interested to hear other people's feedback here as the "right"
solution will be largely dependent on how people would actually be using
tables in shell scripts.

Thanks for bringing this up! -- Eric
Matthias Teege
2010-02-07 09:01:15 UTC
Permalink
On 11:33 Sat 06 Feb, Eric Schulte wrote:

Moin,

thanks for your reply
Post by Eric Schulte
1) allowing the user to specify a separator with a header argument as
follows
#+begin_src sh :var table=sec :separator ,
cat <<EOF
$table
EOF
#+end_src
which would result in something like
: "Hello, World"
I like this idea because it is what I would expect. In my first
"experiment", I assumed that I can use the "|" as a separator. I would
like to use a table as input form and pipe the rows to a shell script
written in org-babel. Sometimes it is easier to proccess data with
awk instead of using elisp. So my example is a bit misleading.
Post by Eric Schulte
2) writing the table to a tab or comma separated file and then
replacing =$table= in the source block body with the path to the
file name, s.t. something like
IMHO it is not necessary under Unix. Reading from stdin is usual. But it
maybe different under Windows.

Many thanks
Matthias
Eric Schulte
2010-02-07 17:20:30 UTC
Permalink
Alright,

I've set this up so that the table is exported to a comma or tab
separated format and then saved as a string to the variable in the
source code block. By default tab is used as the separator when
exporting the table to a string, but this can be overridden using
the :separator header argument.

The following large snippet show some example usage of tables in shell
scripts. Please let me know how it works for you and if you have any
other suggestions.

Thanks -- Eric

--8<---------------cut here---------------start------------->8---
#+tblname: fibs
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
| 5 | 5 |
| 6 | 8 |

#+begin_src sh :var table=fibs
echo "$table" |wc
#+end_src

#+results:
: 6 12 24

#+begin_src sh :var table=fibs
echo "$table"
#+end_src

#+results:
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
| 5 | 5 |
| 6 | 8 |

#+begin_src sh :var table=fibs :separator --
echo "$table" | head -1
#+end_src

#+results:
: 1--1
--8<---------------cut here---------------end--------------->8---
Post by Matthias Teege
Moin,
thanks for your reply
Post by Eric Schulte
1) allowing the user to specify a separator with a header argument as
follows
#+begin_src sh :var table=sec :separator ,
cat <<EOF
$table
EOF
#+end_src
which would result in something like
: "Hello, World"
I like this idea because it is what I would expect. In my first
"experiment", I assumed that I can use the "|" as a separator. I would
like to use a table as input form and pipe the rows to a shell script
written in org-babel. Sometimes it is easier to proccess data with
awk instead of using elisp. So my example is a bit misleading.
Post by Eric Schulte
2) writing the table to a tab or comma separated file and then
replacing =$table= in the source block body with the path to the
file name, s.t. something like
IMHO it is not necessary under Unix. Reading from stdin is usual. But it
maybe different under Windows.
Many thanks
Matthias
_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
http://lists.gnu.org/mailman/listinfo/emacs-orgmode
Loading...