重击-删除所有以'P'开头的行 [英] Bash - remove all lines beginning with 'P'

查看:227
本文介绍了重击-删除所有以'P'开头的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

> cat file.txt | egrep -v P*

这不会输出到控制台.我可以在文件上使用cat,而无需其他命令,它可以很好地打印出来.我的最终目的是:

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 file.txt | egrep -v P* > new.txt

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

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.

推荐答案

说明

  1. 使用^将样式锚定到行的开头;
  2. 使用sed d标志删除与模式匹配的行./li>
  1. use ^ to anchor your pattern to the beginning of the line ;
  2. delete lines matching the pattern using sed and the d flag.

解决方案#1

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

更好的解决方案

仅使用sed:

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

这篇关于重击-删除所有以'P'开头的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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