管道排序为 sed,匹配记录末尾的 5 位或更多数字



File

plym    fury    77      73      2500
chevy   nova    79      60      3000
ford    mustang 65      45      17000
volvo   gl      78      102     9850
ford    ltd     83      15      10500
Chevy   nova    80      50      3500
fiat    600     65      115     450
honda   accord  81      30      6000
ford    thundbd 84      10      17000
toyota  tercel  82      180     750
chevy   impala  65      85      1550
ford    bronco  83      25      9525

我必须删除所有 10,000 美元或以上的汽车(文件中的最后一列)。为此,我必须通过管道将排序的输出传送到 sed 中,方法是在记录末尾匹配表示 5 位或更多数字的正则表达式时退出。

这是我必须通过管道传输的命令:grep -iv chevy cars | sort -nk 5

到目前为止我尝试过:

grep -iv chevy cars | sort -nk 5 | sed -n '/[0-9]{5}*/'

但无济于事。


So far we have:
grep -iv chevy cars | sort -nk 5
Now let's 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):
You entered: grep -iv chevy cars | sort -nk 5 | sed -n '/[0-9]{5}$/'
Please try again.

使用-n开关,您必须显式p rint 匹配行,并且!p打印非加工行,即汽车不花费 5 位数值。还要向模式添加 $ 以仅在末尾匹配,并为重复组转义{},否则它将匹配数字后面{5}的文本字符串。

所以试试这个:sed -n '/[0-9]{5}$/!p'

如果要在第一个匹配项时退出,而不是仅打印相关匹配项,请尝试使用 q 命令而不是 !p

grep -iv chevy cars | sed '/[0-9][0-9][0-9][0-9][0-9]$/ d'

尝试

sed '/[0-9][0-9][0-9][0-9][0-9]$/ d'

看起来很乏味,但它有效!

最新更新