从模式中的Grep输出中获取最大索引 [英] Grab Largest Index From Grep Output In Pattern
问题描述
我的同事今天有一个有趣的问题, 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 forsort
- 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屋!