返回数据直到字符 X 第 N 次出现
在一个目录中,我有多个文件,这些文件内有多个版本号。我正在 grep 目录中的每个文件中查找这些版本号,对它们进行排序以获得最新的版本号,然后将其通过管道传输到“tail -1”中,仅显示最新的版本号,而不是每个 grep 结果。
数据看起来像这样:
file1: asdfgarbage 1.2.4.1garbagetext asdf
file2: fsdafgarbage asdfsda 4.3.2.10 fdsaf
等等。我已经完成提取最新版本号。我是这样做的:
grep -o '[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}' * | sort | tail -1
下一部分是我遇到的麻烦。我试图在第一个句点之前提取数字(无论是一个数字字符还是两个数字字符)并返回该结果。然后,我假设使用稍微不同的命令做同样的事情,但对于第一个周期之后的数字。再次计算第二节后的数字,最后是第三节后的数字。
我对 sed 或 awk 几乎没有经验,但经过一番研究后,我相信这些工具中的任何一个都是实现这一目标的方法。
谢谢你!
编辑:好吧,我明白了,但我确信这可以通过更简单的方式完成。我所拥有的是以下内容:
grep -o '[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}' * | sort | tail -1 | grep -o '[0-9]\{1,\}' | sed -n 2p
或 sed -n 1p, 3p, 4p 取决于我想要的值。
Withing a directory I have multiple files that have multiple version numbers within the files. I am grepping each file within the directory for these version numbers, sorting them in order to get the most recent version number, and then piping that into 'tail -1' to only the most recent version number and not every grep result.
The data looks something like this:
file1: asdf garbage 1.2.4.1 garbagetext asdf
file2: fsdaf garbage asdfsda 4.3.2.10 fdsaf
and so on. I have already accomplished extracting the most recent version number. I did this with the following:
grep -o '[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}' * | sort | tail -1
The next part is what I am having trouble on. I am trying to extract the number (whether it be one number character or two number characters) before the first period and return that result. Then, I am assuming with a slightly different command do the same thing but for the number after the first period. And again for the number after the second period and finally after the third period.
I have little to no experience with sed or awk, but after a little research I believe either one of these tools are the way to accomplish this.
Thank you!
Edit: Alright I got it, but I am certain this can be done in a much easier way. What I have is the following:
grep -o '[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}' * | sort | tail -1 | grep -o '[0-9]\{1,\}' | sed -n 2p
or sed -n 1p, 3p, 4p depending on which value I want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
获取最新版本号:
提取数字:
您可以将这两行放在一起。
to get the lastest version number:
to extract numbers:
you can put the two line together.
要提取版本号,而不必知道其中有多少个点,我会使用
(假设您有 GNU 排序,带有
--version-sort
选项)来获取主要版本号,将上述内容通过管道传输到其中之一
To extract a version number, without having to know how many dots are in it, I would use
(assuming you have GNU sort, with the
--version-sort
option)To get just the major version number, pipe the above into one of