Bash - 删除所有以“P”开头的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17208931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:44:16  来源:igfitidea点击:

Bash - remove all lines beginning with 'P'

bash

提问by user2450099

I have a text file that's about 300KB in size. I want to remove all lines from this file that begin with the letter "P". This is what I've been using:

我有一个大约 300KB 的文本文件。我想从这个文件中删除所有以字母“P”开头的行。这是我一直在使用的:

> cat file.txt | egrep -v P*

That isn't outputting to console. I can use cat on the file without another other commands and it prints out fine. My final intention being to:

那不是输出到控制台。我可以在没有其他命令的情况下在文件上使用 cat 并且它打印得很好。我的最终目的是:

> cat file.txt | egrep -v P* > new.txt

No error appears, it just doesn't print anything out and if I run the 2nd command, new.txt is empty. I should say I'm running Windows 7 with Cygwin installed.

没有出现错误,它只是不打印任何内容,如果我运行第二个命令,则 new.txt 为空。我应该说我正在运行安装了 Cygwin 的 Windows 7。

回答by édouard Lopez

Explanation

解释

  1. use ^to anchor your pattern to the beginning of the line ;
  2. delete lines matching the pattern using sedand the dflag.
  1. 用于^将您的模式锚定到行首;
  2. 删除匹配模式 usingseddflag 的行

Solution #1

解决方案#1

cat file.txt | sed '/^P/d'

Better solution

更好的解决方案

Use sed-only:

sed仅使用:

sed '/^P/d' file.txt > new.txt

回答by Zulu

With awk:

使用 awk:

awk '!/^P/' file.txt

Explanation

解释

  1. The condition starts with an !(negation), that negatesthe following pattern ;
    • /^P/means "match all lines starting with a capital P",
  2. So, the pattern is negated to "ignore lines starting with a capital P".
  3. Finally, it leverage awk's behavior when { … }(action block) is missing, that is to print the record validating the condition.
  1. 条件以!(否定)开头,否定以下模式;
    • /^P/意思是“匹配所有以大写开头的行P
  2. 因此,该模式被否定为忽略以大写开头的行P
  3. 最后,awk{ … }( action block) 丢失时,它会利用的行为,即打印验证条件的记录。

So, to rephrase, it ignores lines starting with a capital Pand print everything else.

因此,换言之,它会忽略以大写开头的行P并打印所有其他内容

Note

笔记

sedis line oriented and awkcolumn oriented. For your case you should use the first one, see Edouard Lopez's reponse.

sed是面向行和awk面向列的。对于您的情况,您应该使用第一个,请参阅 Edouard Lopez 的回复。

回答by Grzegorz ?ur

Use start of line mark and quotes:

使用行首标记和引号:

 cat file.txt | egrep -v '^P.*'

P*means P zero or more times so together with -vgives you no lines

P*意味着 P 零次或多次,所以连同-v给你没有线

^P.*means start of line, then P, and any char zero or more times

^P.*表示行首,然后是 P 和任何字符零次或多次

Quoting is needed to prevent shell expansion.

需要引用以防止外壳扩展。

This can be shortened to

这可以缩短为

egrep -v ^P file.txt

because .*is not needed, therefore quoting is not needed and egrepcan read data from file.

因为.*不需要,因此不需要引用并且egrep可以从文件中读取数据。

As we don't use extended regular expressions grepwill also work fine

由于我们不使用扩展的正则表达式grep也可以正常工作

grep -v ^P file.txt

Finally

最后

grep -v ^P file.txt > new.txt

回答by Prasanth

This works:

这有效:

cat file.txt | egrep -v -e '^P'

-eindicates expression.

-e表示表达。

回答by bartimar

Use sedwith inplace substitution (for GNU sed, will also for your cygwin)

sed与就地替换一起使用(对于 GNU sed,也将用于您的 cygwin)

sed -i '/^P/d' file.txt

BSD (Mac) sed

BSD (Mac) sed

sed -i '' '/^P/d' file.txt