emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* How to get a table into a variable in a shell code block?
@ 2021-04-03 18:14 William Denton
  2021-04-03 18:45 ` Greg Minshall
  0 siblings, 1 reply; 5+ messages in thread
From: William Denton @ 2021-04-03 18:14 UTC (permalink / raw)
  To: emacs-orgmode

Let's say I have a table like this:

#+NAME: numbers
| one   |
| two   |
| three |

I want to run through those numbers in a shell code block, but it I pass in 
table as a variable, it only sees the first number.

#+begin_src shell :results output :var n=numbers
echo $n
#+end_src

#+RESULTS:
: one

In Ruby it sees the numbers as an array:

#+begin_src ruby :results output :var n=numbers
puts n
puts n.class
#+end_src

#+RESULTS:
: one
: two
: three
: Array

And in R sees them as a data.table with one column.  Both are very easy to 
iterate over, of course.

I looked at the docs, but didn't see this covered, then I looked at ob-shell.el 
and saw the "org-babel--variable-assignments:bash_array" function, which might 
or might not be relevant, but it's all beyond my comprehension.

Is there a way to get my shell code seeing all of the elements in the column?

Thanks,

Bill
--
William Denton :: Toronto, Canada   ---   Listening to Art: https://listeningtoart.org/
https://www.miskatonic.org/         ---   GHG.EARTH: https://ghg.earth/
Caveat lector.                      ---   STAPLR: https://staplr.org/


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

* Re: How to get a table into a variable in a shell code block?
  2021-04-03 18:14 How to get a table into a variable in a shell code block? William Denton
@ 2021-04-03 18:45 ` Greg Minshall
  2021-04-03 18:57   ` William Denton
  2021-04-03 19:01   ` Juan Manuel Macías
  0 siblings, 2 replies; 5+ messages in thread
From: Greg Minshall @ 2021-04-03 18:45 UTC (permalink / raw)
  To: William Denton; +Cc: emacs-orgmode

William,

try

#+begin_src shell :results output :var n=numbers
echo ${n[1]}
#+end_src

cheers, Greg


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

* Re: How to get a table into a variable in a shell code block?
  2021-04-03 18:45 ` Greg Minshall
@ 2021-04-03 18:57   ` William Denton
  2021-04-04  5:26     ` Michael Welle
  2021-04-03 19:01   ` Juan Manuel Macías
  1 sibling, 1 reply; 5+ messages in thread
From: William Denton @ 2021-04-03 18:57 UTC (permalink / raw)
  To: Greg Minshall; +Cc: emacs-orgmode

On 3 April 2021, Greg Minshall wrote:

> #+begin_src shell :results output :var n=numbers
> echo ${n[1]}
> #+end_src

Aha, it's in an array but I didn't see it!  Thanks.  Org was doing it magic but 
I got confused by bash.

#+NAME: numbers
| one   |
| two   |
| three |

#+begin_src shell :results output :var n=numbers
for i in "${n[@]}"; do
     echo $i
done
#+end_src

#+RESULTS:
: one
: two
: three

Bill
--
William Denton :: Toronto, Canada   ---   Listening to Art: https://listeningtoart.org/
https://www.miskatonic.org/         ---   GHG.EARTH: https://ghg.earth/
Caveat lector.                      ---   STAPLR: https://staplr.org/


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

* Re: How to get a table into a variable in a shell code block?
  2021-04-03 18:45 ` Greg Minshall
  2021-04-03 18:57   ` William Denton
@ 2021-04-03 19:01   ` Juan Manuel Macías
  1 sibling, 0 replies; 5+ messages in thread
From: Juan Manuel Macías @ 2021-04-03 19:01 UTC (permalink / raw)
  To: Greg Minshall; +Cc: orgmode

Hi Greg and William,

Greg Minshall writes:

> William,
>
> try
>
> #+begin_src shell :results output :var n=numbers
> echo ${n[1]}
> #+end_src
>
> cheers, Greg

I don't know if I'm saying something wrong, but wouldn't it be better
this way?:

#+begin_src shell :results output :var n=numbers
echo ${n[@]}
#+end_src

echo ${n[1]} returns the second element (two) of the list (0, returns
one and 2 returns three)

Best regards,

Juan Manuel 




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

* Re: How to get a table into a variable in a shell code block?
  2021-04-03 18:57   ` William Denton
@ 2021-04-04  5:26     ` Michael Welle
  0 siblings, 0 replies; 5+ messages in thread
From: Michael Welle @ 2021-04-04  5:26 UTC (permalink / raw)
  To: emacs-orgmode

Hello,

William Denton <wtd@pobox.com> writes:

> On 3 April 2021, Greg Minshall wrote:
>
>> #+begin_src shell :results output :var n=numbers
>> echo ${n[1]}
>> #+end_src
>
> Aha, it's in an array but I didn't see it!  Thanks.  Org was doing it magic but
> I got confused by bash.
interestingly I asked a similar question in #org-mode yesterday. I came
up with this approach:

* table test
** user data
#+name: userdata
| uid | name | foo   | bar  |
| a2s | a2   | afoo  | abar |
| b2s | b2   | bfoo  | bbar |
| c2s | c2   | c foo | cbar |

** code
#+begin_src sh :var userdata=userdata :separator , :results raw
  echo "$userdata" | while read line ; do
      IFS=','
      echo "$line" | while read u n f b ; do
          echo "u: $u n: $n f: $f b: $b"
      done
  done
#+end_src

This looks a bit clumsy, but should also work with shell that doesn't
support (2D) array type variables.

Regards
hmw


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

end of thread, other threads:[~2021-04-04  5:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-04-03 18:14 How to get a table into a variable in a shell code block? William Denton
2021-04-03 18:45 ` Greg Minshall
2021-04-03 18:57   ` William Denton
2021-04-04  5:26     ` Michael Welle
2021-04-03 19:01   ` Juan Manuel Macías

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).