Emacs,删除所有不匹配的行? [英] Emacs, removing all lines that don't match?

查看:158
本文介绍了Emacs,删除所有不匹配的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文本文件中进行大量更改时,我会定期使用(query-replace-regexpfromto)表达



我想要一个正则表达式(如果存在),用于删除匹配的所有行。例如,在用于构建RPM的RedHat SPEC文件中,我想离开 / ^ Patch / 开头的行(和删除所有不匹配的行)。很简单, grep -E'^ Patch'但Emacs中有没有办法?



我试过: / p>

 (query-replace-regexp^ \\(?!Patch\\)[^ \r\\ \\ n] * $)

无效(负显示不显示不受支持) / p>

任何想法?

解决方案

尝试 Mx保持线^ Patch 代替:


(keep-lines REGEXP&可选RSTART REND INTERACTIVE) code>



删除包含REGEXP匹配项的行除外。


还有一个相反的命令,即 Mx flush-lines ,它删除与正则表达式匹配的行。


I use the (query-replace-regexp "from" "to") expression regularly when making large changes in a text file.

I'd like a regular expression, if one exists, for removing all lines that don't match. For example, in a RedHat SPEC file for building a RPM I want to leave just the lines that begin with /^Patch/ in them (and delete all non-matching lines). Easy enough with grep -E '^Patch' but is there a way in Emacs?

I tried:

(query-replace-regexp "^\\(?!Patch\\)[^\r\n]*$" "")

to no avail (negative-lookahead appears unsupported).

Any ideas?

解决方案

Try M-x keep-lines ^Patch instead:

(keep-lines REGEXP &optional RSTART REND INTERACTIVE)

Delete all lines except those containing matches for REGEXP.

There is also the opposite command, M-x flush-lines, which removes lines matching a regexp.

这篇关于Emacs,删除所有不匹配的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆