使用 Emacs 交换 2 列
我有 2 列,用逗号分隔。如何与 Emacs 交换这些列?
我有以下内容:
column 1,column2
x1,x2
y1,y2
f1,f2
我想要这样的:
column2,column 1
x2,x1
y2,y1
f2,f1
I have 2 columns, separated by comma. How can I swap those columns with Emacs?
I have the following:
column 1,column2
x1,x2
y1,y2
f1,f2
and I want it like this:
column2,column 1
x2,x1
y2,y1
f2,f1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
Mx query-replace-regexp
,然后:作为替换正则表达式并
进行替换。
在 Emacs 中,您需要使用
\
转义分组括号。因此,上面的正则表达式通常会写成这意味着您希望第一组中逗号之前的所有内容以及第二组中逗号之后的所有内容。
意思是:写第二组,然后写逗号,然后写第一组。
Use
M-x query-replace-regexp
and then:as replace regexp and
for replacement.
In Emacs, you need to escape grouping parentheses with
\
. So, above regexp would be usually written aswhich means that you want everything before comma in first group and everything after comma in second group.
means: write second group, then comma, then first group.
虽然您可以应用其他人提供的技术,但您也可以使用组织模式表。
将数据转换为组织模式表后,通过简单的按键即可轻松交换列。您可以使用 Mx org-mode,选择区域,然后执行 Mx org-table-convert-region,然后在最右侧的列上执行 M-。我不确定如何将数据导出为 CSV,但是使用 Replace-regexp 对您来说应该很容易。这可能会有所帮助:http://www.gnu。 org/software/emacs/manual/html_node/org/Tables.html#Tables
While you can apply techniques given by other people, you can also use the org-mode tables.
Once you convert the data into org-mode table, it is very easy to swap the columns by simple keystrokes. You can have M-x org-mode, select the region then do M-x org-table-convert-region, and then M- on the right most column. I am not sure, how to export the data as CSV, but that should be very easy for you with replace-regexp. This can be helpful: http://www.gnu.org/software/emacs/manual/html_node/org/Tables.html#Tables
与 @darioo 给出的答案类似,在缓冲区顶部输入以下内容:
然后,将光标放在该行的末尾并按 ctrl-x、ctrl-e。
您将看到一个交互式搜索和替换,您可以按空格键进行更改,然后按 ctrl-g 退出。如果您按
!
(感叹号),则搜索将停止交互,并在所有匹配的文本上进行。如果您想撤消更改,请按 Mx(通常按 ESC,然后按
x
),然后输入undo
,然后按 Enter。Similar to the answer given by @darioo, type the following into the top of your buffer:
Then, put your cursor at the end of this line and press ctrl-x, ctrl-e.
You will have an interactive search-and-replace for which you press the space bar to make the change, and press ctrl-g to quit. If you press
!
(exclamation mark) then the search will cease being interactive and take place on all matching text.If you want to reverse the changes then press M-x (usually ESC followed by
x
) and typeundo
and press enter.Emacs 有一个矩形选择模式,例如: http: //emacs-fu.blogspot.com/2008/12/working-with-rectangle-selections.html
更好的是,如果启用 cua-mode,输入 Ctrl-Enter 将使您进入矩形选择模式,即非常容易使用。
http://trey-jackson.blogspot .com/2008/10/emacs-tip-26-cua-mode-specially.html
Emacs has a rectangular selection mode, see for example: http://emacs-fu.blogspot.com/2008/12/working-with-rectangular-selections.html
Even better, if you enable cua-mode, entering Ctrl-Enter will put you in rectangle selection mode that is very easy to use.
http://trey-jackson.blogspot.com/2008/10/emacs-tip-26-cua-mode-specifically.html
使用宏!
选择其余行和:
更新:如果一列中有多个单词,则此方法无法正常工作。寻找更通用的解决方案...
Use a macro !
Select the rest of the lines and:
UPDATE: This doesn't work properly if you have multiple words in a column. Looking for a more general solution...