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

时间:2021-04-26 22:20:09

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.

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

7 个解决方案

#1


46  

In VIM:

在VIM中:

:g!/^[+-]/d

: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.

全局地对所有不做的行做一些事情!匹配正则表达式:开始行^ +或-,这是删除这些行。

#2


1  

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

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

#3


0  

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

diff - u <一些参数> | grep的^(+ -)”

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

或者你根本不能生产额外的生产线:

diff --unified=0 <some args>

diff -统一= 0 <一些参数>

#4


0  

cat your_diff_file | sed '/^[+-]/!D'

#5


0  

egrep "^[+-]" difffile >outputfile

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

不要删除所有不匹配的内容,只显示匹配的行。:)

#6


0  

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

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

it also provides code examples for many different languages.

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

#7


0  

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

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

它是否内联在当前文件上,并且对于大文件可以比v快得多。

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

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

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

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

#1


46  

In VIM:

在VIM中:

:g!/^[+-]/d

: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.

全局地对所有不做的行做一些事情!匹配正则表达式:开始行^ +或-,这是删除这些行。

#2


1  

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

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

#3


0  

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

diff - u <一些参数> | grep的^(+ -)”

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

或者你根本不能生产额外的生产线:

diff --unified=0 <some args>

diff -统一= 0 <一些参数>

#4


0  

cat your_diff_file | sed '/^[+-]/!D'

#5


0  

egrep "^[+-]" difffile >outputfile

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

不要删除所有不匹配的内容,只显示匹配的行。:)

#6


0  

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

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

it also provides code examples for many different languages.

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

#7


0  

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

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

它是否内联在当前文件上,并且对于大文件可以比v快得多。

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

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

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

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