从bash输出中排除一个字符串 [英] Exclude one string from bash output

查看:42
本文介绍了从bash输出中排除一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个项目.在这个项目中,由于某些原因,我需要从与模式匹配的输出(或文件)中排除第一个字符串.困难在于我只需要从流中排除一个字符串,仅排除第一个字符串.例如,如果我有:

I'm working now on a project. In this project for some reasons I need to exclude first string from the output (or file) that matches the pattern. The difficulty is in that I need to exclude just one string, just first string from the stream. For example, if I have:

1 abc
2 qwerty
3 open
4 abc
5 talk

一些脚本工作后,我应该有这个:

After some script working I should have this:

2 qwerty
3 open
4 abc
5 talk

注意:我对单词之前的数字一无所知,因此无法使用有关它们的知识来过滤输出.

NOTE: I don't know anything about digits before words, so I can't filter the output using knowledge about them.

我已经用grep编写了小脚本,但是它切出了与模式匹配的每个字符串:

I've written small script with grep, but it cuts out every string, that matches the pattern:

'some program' | grep -v "abc"

阅读有关awk,sed等的信息,但不知道我是否可以解决我的问题.有什么帮助,谢谢.

Read info about awk, sed, etc. but didn't understand if I can solve my problem. Anything helps, Thank you.

推荐答案

使用awk:

some program | awk '{ if (/abc/ && !seen) { seen = 1 } else print }'

或者,仅使用过滤器:

some program | awk '!/abc/ || seen { print } /abc/ && !seen { seen = 1 }'

这篇关于从bash输出中排除一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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