emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [Babel] Table collapsed as one big line, when passed to a shell script
@ 2010-11-23 11:00 Sébastien Vauban
  2010-11-23 14:24 ` Eric Schulte
  0 siblings, 1 reply; 2+ messages in thread
From: Sébastien Vauban @ 2010-11-23 11:00 UTC (permalink / raw)
  To: emacs-orgmode-mXXj517/zsQ

#+TITLE:     Line breaks preservation
#+DATE:      2010-11-23
#+LANGUAGE:  en_US

* Abstract

Table is seen as being *one big line*, when echo'ing all of its rows.

* Passing var via Babel

I want to *add a column* to the following table.

#+results: table-message
| This is line 1 of the message.        |
| This is line 2 of the message.        |
| This is the last line of the message. |

Its value should be dependant on a *regexp matching* the *current row* (for
example, if 1 is detected in the original column, then write "A" in the new
one, "B" if 2 is read, "C" if 3 is read, etc.).

Hence, I'm thinking using AWK as an easy solution.

    #+begin_src note
    I'm open to other ideas on how I could do this as easily. Just throw me
    ideas, if you have some.
    #+end_src

*First* trial: add a column whose cell contents will be *fixed* (here, equal
to =New col=).

#+srcname: add-col
#+begin_src sh :var data=table-message :results output :exports both
echo $data | awk '// {print "| New col | " $0 " |";}'
#+end_src

#+results: add-col
: | New col | This is line 1 of the message. This is line 2 of the message. This is the last line of the message. |

I was expecting 3 lines, not 1...

* Replacing Babel expansion of the variable

Here, I made a few changes:

- added option =-n= to cat, to make him number the lines

- added explicit =[BEGINOFLINE]= and =[ENDOFLINE]= markers to see where the
  lines begin and end

#+srcname: add-col-expanded
#+begin_src sh :results output :exports both
data=$(cat -n <<BABEL_TABLE
This is line 1 of the message.
This is line 2 of the message.
This is the last line of the message.
BABEL_TABLE
)
echo $data | awk '// {print "[BEGINOFLINE]| New col | " $0 " |[ENDOFLINE]";}'
#+end_src

#+results: add-col-expanded
: [BEGINOFLINE]| New col | 1 This is line 1 of the message. 2 This is line 2 of the message. 3 This is the last line of the message. |[ENDOFLINE]

Still the same, though we observe that =cat= sees 3 lines, but the =echo= does
not seem to preserve the line breaks, when executed.

* Questions

- Do you have the same problems on your machine?
- Is it due to Cygwin Bash on Windows (my case)?
- Any idea on what could cause this, or on any workaround?

Best regards,
  Seb

-- 
Sébastien Vauban


_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode-mXXj517/zsQ@public.gmane.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

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

* Re: [Babel] Table collapsed as one big line, when passed to a shell script
  2010-11-23 11:00 [Babel] Table collapsed as one big line, when passed to a shell script Sébastien Vauban
@ 2010-11-23 14:24 ` Eric Schulte
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Schulte @ 2010-11-23 14:24 UTC (permalink / raw)
  To: Sébastien Vauban; +Cc: emacs-orgmode

Hi Seb,

Sébastien Vauban <wxhgmqzgwmuf@spammotel.com> writes:

> #+TITLE:     Line breaks preservation
> #+DATE:      2010-11-23
> #+LANGUAGE:  en_US
>
> * Abstract
>
> Table is seen as being *one big line*, when echo'ing all of its rows.
>
> * Passing var via Babel
>
> I want to *add a column* to the following table.
>
> #+results: table-message
> | This is line 1 of the message.        |
> | This is line 2 of the message.        |
> | This is the last line of the message. |
>
> Its value should be dependant on a *regexp matching* the *current row* (for
> example, if 1 is detected in the original column, then write "A" in the new
> one, "B" if 2 is read, "C" if 3 is read, etc.).
>
> Hence, I'm thinking using AWK as an easy solution.
>
>     #+begin_src note
>     I'm open to other ideas on how I could do this as easily. Just throw me
>     ideas, if you have some.
>     #+end_src
>

the easiest (for me) would be with the elisp =mapcar= function
#+begin_src emacs-lisp :var tbl=table-message
  (mapcar (lambda (row) (cons "New col" row)) tbl)
#+end_src

#+results:
| New col | This is line 1 of the message.        |
| New col | This is line 2 of the message.        |
| New col | This is the last line of the message. |

>
> *First* trial: add a column whose cell contents will be *fixed* (here, equal
> to =New col=).
>
> #+srcname: add-col
> #+begin_src sh :var data=table-message :results output :exports both
> echo $data | awk '// {print "| New col | " $0 " |";}'
> #+end_src
>

This is a bash problem(feature) you must wrap the $data variable in
quotes for newlines to be preserved.  Note that in zsh (my preferred
shell) this quote wrapping is not required.

#+srcname: add-col
#+begin_src sh :var data=table-message :results output raw :exports both
echo "$data" | awk '// {print "| New col | " $0 " |";}'
#+end_src

#+results: add-col
| New col | This is line 1 of the message.        |
| New col | This is line 2 of the message.        |
| New col | This is the last line of the message. |

Best -- Eric

>
>
> #+results: add-col
> : | New col | This is line 1 of the message. This is line 2 of the
> message. This is the last line of the message. |
>
> I was expecting 3 lines, not 1...
>
> * Replacing Babel expansion of the variable
>
> Here, I made a few changes:
>
> - added option =-n= to cat, to make him number the lines
>
> - added explicit =[BEGINOFLINE]= and =[ENDOFLINE]= markers to see where the
>   lines begin and end
>
> #+srcname: add-col-expanded
> #+begin_src sh :results output :exports both
> data=$(cat -n <<BABEL_TABLE
> This is line 1 of the message.
> This is line 2 of the message.
> This is the last line of the message.
> BABEL_TABLE
> )
> echo $data | awk '// {print "[BEGINOFLINE]| New col | " $0 " |[ENDOFLINE]";}'
> #+end_src
>
>
> #+results: add-col-expanded
> : [BEGINOFLINE]| New col | 1 This is line 1 of the message. 2 This is
> line 2 of the message. 3 This is the last line of the
> message. |[ENDOFLINE]
>
> Still the same, though we observe that =cat= sees 3 lines, but the =echo= does
> not seem to preserve the line breaks, when executed.
>
> * Questions
>
> - Do you have the same problems on your machine?
> - Is it due to Cygwin Bash on Windows (my case)?
> - Any idea on what could cause this, or on any workaround?
>
> Best regards,
>   Seb

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

end of thread, other threads:[~2010-11-23 14:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-11-23 11:00 [Babel] Table collapsed as one big line, when passed to a shell script Sébastien Vauban
2010-11-23 14:24 ` Eric Schulte

Code repositories for project(s) associated with this 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).