如何删除所有不以某些字符开头的行?

发布于 2024-08-11 00:09:58 字数 90 浏览 5 评论 0 原文

我需要找出一个正则表达式来删除所有不以“+”或“-”开头的行。

我想打印一个大 diff 文件的纸质副本,但它在实际 diff 前后显示 5 行左右。

I need to figure out a regular expression to delete all lines that do not begin with either "+" or "-".

I want to print a paper copy of a large diff file, but it shows 5 or so lines before and after the actual diff.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

萌辣 2024-08-18 00:09:58

在 VIM 中:

:g!/^[+-]/d

这是英文翻译:

globally 对所有不执行操作的行! 匹配正则表达式:行开头 ^ 后跟 +-,要做的就是 d删除这些行。

In VIM:

:g!/^[+-]/d

Here is the English translation:

globally do something to all lines that do NOT! match the regular expression: start of line^ followed by either + or -, and that something to do is to delete those lines.

南街女流氓 2024-08-18 00:09:58

sed -e '/^[^+-]/d'

sed -e '/^[^+-]/d'

酒绊 2024-08-18 00:09:58

diff -u <这里有一些参数> | grep '^[+-]'

或者您根本无法生成额外的行:

diff --unified=0

diff -u <some args here> | grep '^[+-]'

Or you could just not produce the extra lines at all:

diff --unified=0 <some args>

幻想少年梦 2024-08-18 00:09:58
cat your_diff_file | sed '/^[+-]/!D'
cat your_diff_file | sed '/^[+-]/!D'
云淡风轻 2024-08-18 00:09:58
egrep "^[+-]" difffile >outputfile

您不会删除所有不匹配的内容,而是仅显示匹配的行。 :)

egrep "^[+-]" difffile >outputfile

Instead of deleting everything that doesn't match you show only lines that match. :)

风追烟花雨 2024-08-18 00:09:58

如果您需要用正则表达式做一些更复杂的事情,您应该使用这个网站:
http://txt2re.com/

它还提供了许多不同语言的代码示例。

If you need to do something more complex in terms of regular expressions, you should use this site:
http://txt2re.com/

it also provides code examples for many different languages.

初见终念 2024-08-18 00:09:58
%!grep -Ev '^[+-]'

它内联在当前文件上,并且对于大文件来说比 :v 快得多。

在 Vim 7.4、Ubuntu 14.04、1M 行日志文件上测试。

不包含单词的行: https://superuser.com/questions/265085/vim-delete-all-lines-that-do-not-contain-a-certain-word/1187212#1187212

%!grep -Ev '^[+-]'

does it inline on current file, and can be considerably faster than :v for large files.

Tested on Vim 7.4, Ubuntu 14.04, 1M line log file.

Lines that don't contain word: https://superuser.com/questions/265085/vim-delete-all-lines-that-do-not-contain-a-certain-word/1187212#1187212

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文