Question

I have some org table with 5 columns and 100 rows.

I would like to delete all those rows with field $3="away".

Below is the function that should do the following. Unfortunately I do not know how to convert org table that is created with (org-table-to-lisp), back to text table into my buffer.

(defun org-table-tests ()
  "acts on balance table itself"
  (interactive)
  (save-excursion
    (unless (org-table-p)
      (error
       "You are not in an org-table."))
    (goto-char (org-table-begin))
    (let (out-tbl
          (tbl-list (org-table-to-lisp)))
      (while
          (let ((row-list (car tbl-list)))
            (cond ((and
                    (listp row-list)
                    ;; do not copy "away" (3rd column) lines containing folliwng:
                    (string-match "away" (nth 2 row-list)))
                   ;; just skip copying to new table
                   )
                  (t (setq out-tbl (cons row-list out-tbl))))
            (setq tbl-list (cdr tbl-list))))
      ;;(orgtbl-to-generic out-tbl)  <---------- QUESTION: which function to use to convert back to table in text format.
      )))

... or maybe there is a better way for doing such tasks with orgtbl mode?

Was it helpful?

Solution

Since your main question was answered, here's now a solution that doesn't rely on converting to lisp and back :

(defun org-table-tests ()
  (interactive)
  (save-excursion
    (unless (org-table-p)
      (error "You are not in an org-table."))
    (goto-char (org-table-begin))
    (while (org-table-p)
      (if (string-match "away" (save-excursion (org-table-get-field 3)))
          (delete-region (point) (progn (forward-line) (point)))
        (forward-line)))))

OTHER TIPS

Have a look at org-listtable-to-string. It's the reverse of org-table-to-lisp.

Also have a look at this screencast to see how I've found the solution in under a minute (just grepped for "to table" in org's code base, basically).

If you just want to remove row where $3=="away", following commands are useful. But I don't know why it works.

ESC M-<

M-x flush-lines

RET

^[|][^|]*[|][^|
]*[|] away [|]

RET

Input:

|    1 |    2 |    3 |    4 |    5 |
|------+------+------+------+------|
| away |   12 |   13 |   14 | away |
|   21 |   22 | away |   24 |   25 |
|   31 | away |   33 | away |   35 |

Output:

|    1 |    2 |    3 |    4 |    5 |
|------+------+------+------+------|
| away |   12 |   13 |   14 | away |
|   31 | away |   33 | away |   35 |
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top