5

How can I insert a horizontal line (hline) in a bash/sh source code block? My goal is to separate the header line from the rest.

#+begin_src sh
echo "header1,header2"
echo "???"  # placeholder for the magic value
echo "value1,value2"
echo "value1,value2"
#+end_src

The desired outcome would be

#+RESULTS:
| header1 | header2 |
|---------+---------|
| value1  | value2  |
| value1  | value2  |

In a Python src block I can insert a None-Value and get an hline. I hope there is a possibility.

Edit: @jagrg answers solves the problem to separate the header from the rest I would also to insert a hline at any point in the table. Here is the expected outcome.

#+begin_src bash :colnames '("header1" "header2")
echo "value1,value2"
echo "value1,value2"
echo "???" # magic value
echo "value1,value2"
#+end_src

#+RESULTS:
| header1 | header2 |
|---------+---------|
| value1  | value2  |
| value1  | value2  |
|---------+---------|
| value1  | value2  |

I tried echo hline and echo "hline" with no success. I also looked in the code of ob-shell.el and it looks that a simple echo hline should do it but it doesn't.

2 Answers 2

4

You can specify column names as header arguments (see here).

#+begin_src sh :colnames '("header1" "header2")
echo "value1,value2"
echo "value1,value2"
#+end_src

#+RESULTS:
| header1 | header2 |
|---------+---------|
| value1  | value2  |
| value1  | value2  |

To answer your second question, here's a solution you can try using sed (see this thread).

#+begin_src sh :results raw
(
echo "header,header2"
echo ","
echo "value1,value2"
echo "value1,value2"
echo ","
echo "value1,value2"
) | sed -re 's/^|,/|/g' -e 's/^\|$/|-/'
#+end_src

#+RESULTS:
| header | header2 |
|--------+---------|
| value1 | value2  |
| value1 | value2  |
|--------+---------|
| value1 | value2  |
0

If you don't want to rewrite your script to use raw output you also can use the :post parameter to do add hlines during post-processing. This can also be combined with :colnames. I.e.

#+name: add-footer
#+begin_src elisp :var table=""
(append (butlast table) '(hline) (last table))
#+end_src

#+name: add-hlines
#+begin_src elisp :var table="" :hlines yes :colnames true
(cl-map 'list
        (lambda (r) (if (and (listp r) (equal (car r) "hline"))
                        'hline
                      r))
        table)
#+end_src

Add a hline at a fixed position

#+begin_src bash :colnames '("header1" "header2") :post add-footer(*this*)
echo "value1,value2"
echo "value2,value3"
echo "value5,value4"
#+end_src

#+RESULTS:
| header1 | header2 |
|---------+---------|
| value1  | value2  |
| value2  | value3  |
|---------+---------|
| value5  | value4  |

Or use a 'hline' keyword for dynamic positions

#+begin_src bash :colnames '("header1" "header2") :post add-hlines(*this*)
echo "value1,value2"
echo "value3,value4"
echo "hline," # magic value
echo "value5,value6"
echo "value7,value8"
#+end_src

#+RESULTS:
| header1 | header2 |
|---------+---------|
| value1  | value2  |
| value3  | value4  |
|---------+---------|
| value5  | value6  |
| value7  | value8  |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.