开发者

Emacs, removing all lines that don't match?

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.


use M-x delete-non-matching-lines and M-x delete-matching-lines


If it's just "Patch" you are looking for, you can identify lines not matching ^Patch by using

^([^P]|P[^a]|Pa[^t]|Pat[^c]|Patc[^h])[^\r\n]*$

I admit, it is rather ugly, though. =)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜