Given delimited data in the following format, how can I insert and delete columns?
abc|efg|123|xyz123abc|yes
xxx|bbb|cc|ddd|no
Say, for example, that I wanted to add a 3rd column with a default value of 1 and remove the 4th column so the data would look like this:
abc|efg|1|123|yes
xxx|bbb|1|cc|no
I was looking into org-mode tables as a starting point, however it puts extra spaces around the data.
| abc | efg | 123 | xyz123abc |
| xxx | bbb | cc | ddd |
| | | | |
Well I just found out how to import data into SES (there are no "ses-import" functions, but it turns out you can simply yank tab-separated data), so this answer is even simpler than using table-capture :) (unless your data contains tabs, in which case you'll need to work around that).
SES is an actual spreadsheet application within Emacs. See its info node for details:
C-hidm ses
RET, or M-: (info "ses")
RET
To process the data:
- Convert your original data to TSV with, say, M-%|RETTABRET!
- Kill that text (e.g. select region, then C-w)
- Create a temporary buffer and change to ses-mode: C-xb
*ses*
RET
M-x ses-mode
RET
(and of course, C-hm for a description of ses-mode, and its keybindings)
- Yank the data into the spreadsheet: C-y
- Move point to the column to delete, and kill it with M-k
- Copy the result back to the kill ring in TSV format: C-xhxt
(i.e. mark-whole-buffer
+ ses-export-tsv
)
- Kill the *ses* buffer with C-xkRET
- Yank the modified TSV data back into the original buffer with C-y
- Convert the tabs back to
|
s with M-%TABRET|RET!
Caveat: I'm actually having issues yanking into SES under my standard config, but this process works fine under emacs -q
. YMMV.
As well as org-mode's table facilities, Emacs has some other table editing features, which will get you most of the way there.
abc|efg|123|xyz123abc|yes
xxx|bbb|cc|ddd|no
Selecting the region followed by
M-x table-capture
RET |
RET $
RET RET 1
RET
gives us:
+---+---+---+---------+---+
|abc|efg|123|xyz123abc|yes|
+---+---+---+---------+---+
|xxx|bbb|cc |ddd |no |
+---+---+---+---------+---+
Now move point inside a cell of the column you wish to delete, and use
M-x table-delete-column
RET
+---+---+---+---+
|abc|efg|123|yes|
+---+---+---+---+
|xxx|bbb|cc |no |
+---+---+---+---+
'Unrecognising' the table will switch off the table editing mode, so that you can safely mess with the formatting.
M-x table-unrecognize-table
RET
Then it's just a matter of search-and-replace to revert back to the original format.
You can delete all the row separators by killing the first row with C-kC-k, and the remainder with M-%C-yRETRET!)
|abc|efg|123|yes|
|xxx|bbb|cc |no |
Then you just need to clean up the row prefix and suffix, and the padding within cells:
M-C-% +|
RET |
RET ! (n.b. there's a space before the +
)
M-C-% ^|\||$
RET RET !
abc|efg|123|yes
xxx|bbb|cc|no
You could record the clean-up steps as a keyboard macro (or write a function) in order to execute them in a single step.
I can think of two ways to update it manually (though that might not be what you're after):
Adding or deleting characters using rectangles:
cua-mode
<C-return> ;; cua-set-rectangle-mark on first row
;; create a rectangle one character wide from first to last rows
;; type '1|' to create your new column.
Record a macro to add/delete a column entry and repeat for the entire file.