如何使用grep / awk / sed打印所有字符,直到某个特定模式(不包括模式本身)。 [英] How can i print the all the characters until a certain pattern(excluding the pattern itself) using grep/awk/sed.

查看:114
本文介绍了如何使用grep / awk / sed打印所有字符,直到某个特定模式(不包括模式本身)。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如

  echoabc-1234a:| grep< do-something> 

只需打印abc-1234a

解决方案

我认为这些更接近你所得到的,但不知道你真的想达到什么目的,这很难说。

  echoabc-1234a:| egrep -o'^ [^:] +'

...虽然这也符合没有冒号。如果你只想要冒号的行,而你必须只使用grep,这可能会工作:

 回声abc-1234a:| grep:| egrep -o'^ [^:] +'

当然, code> echoabc-1234a:是一个可能被多行输入替换的例子。



最小您可以使用的工具可能是 cut

  echoabc-1234a :| cut -d:-f1 

sed 总是可用......

  echoabc-1234a:| sed's / *:。* //'

对于最后一个,如果您只想打印包含冒号的行,将其更改为:

  echoabc-1234a:| sed -ne's / *:。* // p'

你甚至可以做到这一点在纯bash中:

  while read line; 
field =$ {line %%:*}
#做$ field
完成<<<<abc-1234a:
& code>

有关 %% 位的信息,您可以 man bash 并搜索参数扩展
$ b

更新

您说过:


这是第一行中的字符输入之前的冒号。
的输入可以有多行。


grep 可能不是您的最佳选择,那么,因为他们还会打印可能包含冒号的后续行的数据。当然,还有很多方法可以解决这个需求。我们将从样本输入开始:

  $ function sample {printfabc-1234a:foo \\\
bar baz:\ nNarf\\\
; }
$ sample
abc-1234a:foo
bar baz:
Narf

您可以使用多个管道,例如:

  $ sample |头-1 | grep -Eo'^ [^:] *'
abc-1234a
$ sample |头-1 | cut -d:-f1
abc-1234a

或者你可以用sed来处理第一行:

  $ sample | sed -ne'1s /:.*// p'
abc-1234a

或告诉sed在打印第一行之后退出(这比读取整个文件要快):

  $ sample | sed's /:.*//; q'
abc-1234a

或者做同样的事情,但只显示输出,如果发现冒号(为了安全):

  $ sample | sed -ne's /:.*// p; q'
abc-1234a

或者让awk做同样的事情(分别为最后3个例子):

  $ sample | awk'{sub(/:.*/,))NR == 1'
abc-1234a
$ sample | awk'NR> 1 {nextfile} {sub(/:.*/,)} 1'
abc-1234a
$ sample | awk'NR> 1 {nextfile} sub(/:.*/,)'
abc-1234a

或在bash中,根本没有管道:

  $ read line< <(sample)
$ printf'%s \ n'$ {line %%:*}
abc-1234a


For example

echo "abc-1234a :" | grep <do-something>

to just print abc-1234a

解决方案

I think these are closer to what you're getting at, but without knowing what you're really trying to achieve, it's hard to say.

echo "abc-1234a :" | egrep -o '^[^:]+'

... though this will also match lines that have no colon. If you only want lines with colons, and you must use only grep, this might work:

echo "abc-1234a :" | grep : | egrep -o '^[^:]+'

Of course, this only makes sense if your echo "abc-1234a :" is an example that would be replace with possibly multiple lines of input.

The smallest tool you could use is probably cut:

echo "abc-1234a :" | cut -d: -f1

And sed is always available...

echo "abc-1234a :" | sed 's/ *:.*//'

For this last one, if you only want to print lines that include a colon, change it to:

echo "abc-1234a :" | sed -ne 's/ *:.*//p'

Heck, you could even do this in pure bash:

while read line; do
  field="${line%%:*}"
  # do stuff with $field
done <<<"abc-1234a :"

For information on the %% bit, you can man bash and search for "Parameter Expansion".

UPDATE:

You said:

It's the characters in the first line of input before the colon. The input could have multiple line though.

The solutions with grep probably aren't your best choice, then, since they'll also print data from subsequent lines that might include colons. Of course, there are many ways to solve this requirement as well. We'll start with sample input:

$ function sample { printf "abc-1234a:foo\nbar baz:\nNarf\n"; }
$ sample
abc-1234a:foo
bar baz:
Narf

You could use multiple pipes, for example:

$ sample | head -1 | grep -Eo '^[^:]*'
abc-1234a
$ sample | head -1 | cut -d: -f1      
abc-1234a

Or you could use sed to process only the first line:

$ sample | sed -ne '1s/:.*//p'
abc-1234a

Or tell sed to exit after printing the first line (which is faster than reading the whole file):

$ sample | sed 's/:.*//;q'
abc-1234a

Or do the same thing but only show output if a colon was found (for safety):

$ sample | sed -ne 's/:.*//p;q'
abc-1234a

Or have awk do the same thing (as the last 3 examples, respectively):

$ sample | awk '{sub(/:.*/,"")} NR==1'
abc-1234a
$ sample | awk 'NR>1{nextfile} {sub(/:.*/,"")} 1'
abc-1234a
$ sample | awk 'NR>1{nextfile} sub(/:.*/,"")'
abc-1234a

Or in bash, with no pipes at all:

$ read line < <(sample)
$ printf '%s\n' "${line%%:*}"
abc-1234a

这篇关于如何使用grep / awk / sed打印所有字符,直到某个特定模式(不包括模式本身)。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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