From mboxrd@z Thu Jan 1 00:00:00 1970 From: "Eric Schulte" Subject: Re: [Babel] Table collapsed as one big line, when passed to a shell script Date: Tue, 23 Nov 2010 07:24:37 -0700 Message-ID: <87mxp0nmmy.fsf@gmail.com> References: <80eiacxq2o.fsf@missioncriticalit.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from [140.186.70.92] (port=56894 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PKto5-0006A7-Kw for emacs-orgmode@gnu.org; Tue, 23 Nov 2010 09:25:42 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PKtnf-0004A8-IJ for emacs-orgmode@gnu.org; Tue, 23 Nov 2010 09:25:17 -0500 Received: from mail-qw0-f41.google.com ([209.85.216.41]:50851) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PKtnf-00049v-Ea for emacs-orgmode@gnu.org; Tue, 23 Nov 2010 09:24:51 -0500 Received: by qwj8 with SMTP id 8so52497qwj.0 for ; Tue, 23 Nov 2010 06:24:49 -0800 (PST) In-Reply-To: <80eiacxq2o.fsf@missioncriticalit.com> (=?utf-8?Q?=22S=C3=A9b?= =?utf-8?Q?astien?= Vauban"'s message of "Tue, 23 Nov 2010 12:00:15 +0100") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: =?utf-8?Q?S=C3=A9bastien?= Vauban Cc: emacs-orgmode@gnu.org Hi Seb, S=C3=A9bastien Vauban 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* (f= or > example, if 1 is detected in the original column, then write "A" in the n= ew > 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 =3Dmapcar=3D function #+begin_src emacs-lisp :var tbl=3Dtable-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, eq= ual > to =3DNew col=3D). > > #+srcname: add-col > #+begin_src sh :var data=3Dtable-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=3Dtable-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 =3D-n=3D to cat, to make him number the lines > > - added explicit =3D[BEGINOFLINE]=3D and =3D[ENDOFLINE]=3D markers to see= where the > lines begin and end > > #+srcname: add-col-expanded > #+begin_src sh :results output :exports both > data=3D$(cat -n < 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 =3Dcat=3D sees 3 lines, but the = =3Decho=3D 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