不能把这个简单的sed命令 [英] Cannot get this simple sed command

查看:170
本文介绍了不能把这个简单的sed命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SED 命令描述如下


  

删除为$ 10,000或以上的汽车。管道的输出排序 SED 作为我们匹配正恩$做到这一点,尽快退出p $ pssion重新presenting 5(或以上)的数字达到创纪录的末尾(不要使用重复此):


到目前为止命令是:

  $ grep的-IV雪佛兰汽车|排序-NK 5

我要在该命令我认为其中的终结退出5个或多个数字达到创纪录的结束,只要我们符合常规的前pression重新presenting

我试过类似的东西。

  $ grep的-IV雪佛兰汽车|排序-NK 5 | SED/ [0-9] [0-9] [0-9] [0-9] [0-9] / Q

// 但没有任何工程中其他的变化!什么是匹配的正前pression重新presenting 5个或多个数字和退出根据这个问题的命令?


解决方案

名义上,您应该添加 $ 前的第二个 / 来在记录的末尾匹配5位数字。如果省略 $ ,然后5位数字的序列将导致 SED 退出,所以如果有另一个号码之前报价(VIN一个,也许),它可能匹配,当你没有这打算。

 的grep -IV雪佛兰汽车|排序-NK 5 | SED/ [0-9] [0-9] [0-9] [0-9] [0-9] $ / Q'

从整体上看,它的安全使用单引号周围的正则表达式,除非你需要一个shell变量替换进去(或者除非正则表达式中包含单引号本身)。您还可以指定重复:

 的grep -IV雪佛兰汽车|排序-NK 5 | SED/ [0-9] \\ {5,\\} $ / Q'

\\ {5,\\} 部分5个或多个数字相匹配。如果由于某种原因不工作,你可能会发现你正在使用GNU SED ,你需要做的是这样 SED --posix 来得到它在正常模式下工作。或者您可能能够只是删除反斜杠。当然有选择,GNU 的sed 来改变它使用正则表达式机制(因为有与GNU 的grep 太)。

This sed command is described as follows

Delete the cars that are $10,000 or more. Pipe the output of the sort into a sed to do this, by quitting as soon as we match a regular expression representing 5 (or more) digits at the end of a record (DO NOT use repetition for this):

So far the command is:

$ grep -iv chevy cars | sort -nk 5

I have to add another pipe at the end of that command I think which "quits as soon as we match a regular expression representing 5 or more digits at the end of a record"

I tried things like

$ grep -iv chevy cars | sort -nk 5 | sed "/[0-9][0-9][0-9][0-9][0-9]/ q" 

and other variations within the // but nothing works! What is the command which matches a regular expression representing 5 or more digits and quits according to this question?

解决方案

Nominally, you should add a $ before the second / to match 5 digits at the end of the record. If you omit the $, then any sequence of 5 digits will cause sed to quit, so if there is another number (a VIN, perhaps) before the price, it might match when you didn't intend it to.

grep -iv chevy cars | sort -nk 5 | sed '/[0-9][0-9][0-9][0-9][0-9]$/q'

On the whole, it's safer to use single quotes around the regex, unless you need to substitute a shell variable into it (or unless the regex contains single quotes itself). You can also specify the repetition:

grep -iv chevy cars | sort -nk 5 | sed '/[0-9]\{5,\}$/q'

The \{5,\} part matches 5 or more digits. If for any reason that doesn't work, you might find you're using GNU sed and you need to do something like sed --posix to get it working in the normal mode. Or you might be able to just remove the backslashes. There certainly are options to GNU sed to change the regex mechanism it uses (as there are with GNU grep too).

这篇关于不能把这个简单的sed命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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