从模式中的Grep输出中获取最大索引 [英] Grab Largest Index From Grep Output In Pattern

查看:91
本文介绍了从模式中的Grep输出中获取最大索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的同事今天有一个有趣的问题, grep
她正在制作一个bash脚本,它似乎正在使用 curl 从该处拉动网页,并使用 grep

她的示例输出显示为:

  file-1 
file-10
file-2
file-20
file-3
file-4

通过查看第一个数字并将它们放入其订购。

她最终做出了一个for循环来抓取输出中的最新内容。但它让我好奇。



在上面的例子中,我将如何在一行命令中获得 file-20



。我试过 sort awk 。但是不能正常工作:/ b>

解决方案

您需要:


  • 使用数字排序: -n 标记用于 sort

  • 按数字列排序:使用 -t 指定列分隔符,并使用 -k 指定列

  • 获取最后一行 tail -n1



像这样:

  sort -n -t- -k2 input | tail -n1 


My coworker had an interesting problem today with grep. She is making a bash script that it seems to be pulling a webpage using curl from there and using grep to pull the anchors.

Her example output is coming out as:

    file-1    
    file-10
    file-2 
    file-20
    file-3
    file-4

this is expected as grep sorts by seeing that first numeric and putting them in its "order".

She ended up making a for-loop to grab the latest from the output. But it got me curious.

In the above example how will I obtain the file-20 in one line command?

. I've tried sort and awk. Couldn't get working however :/

解决方案

You need to:

  • Use numeric sort: -n flag for sort
  • Sort by the numeric column: use -t to specify column separator, and -k to specify column
  • Get the last line with tail -n1

Like this:

sort -n -t- -k2 input | tail -n1

这篇关于从模式中的Grep输出中获取最大索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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