bash 从 ls 中提取子字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15939226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:10:13  来源:igfitidea点击:

Extract substring from ls

linuxbashunixsubstring

提问by radicaled

I'm creating a little script and I have to list all my Tomcat binaries.
So far I was able to do this:
ls -1 | grep '\-tomcat\-' | cut -f3 -d'-'

This basically lists all the versions, but it adds the .zip or .tar.gz

我正在创建一个小脚本,我必须列出我所有的 Tomcat 二进制文件。
到目前为止,我能够做到这一点:
ls -1 | grep '\-tomcat\-' | cut -f3 -d'-'

这基本上列出了所有版本,但它添加了 .zip 或 .tar.gz

5.5.17.zip
5.5.26.tar.gz
5.5.27.tar.gz
5.5.28.tar.gz
5.5.31.tar.gz
5.5.32.tar.gz


I would like to know how to remove the .zip and .tar.gz from the extracted strings.


我想知道如何从提取的字符串中删除 .zip 和 .tar.gz。

回答by tink

Or simplify the whole approach:

或者简化整个方法:

ls apache-tomcat*|sed -r 's/^.*-([0-9.]+)\..*//'

Less tools, and it gives you the version numbers.

更少的工具,它为您提供版本号。

P.S.: Following up on @Nemo's suggestion: we let shell globbing and prior knowledge take care of half the job (just list things that actually look like apache-tomcat). When piping ls' output to another tool the -1 is moot, so we get rid of that. sed takes the values coming from ls, matches beginning of line to the first - followed by a digit, the parenthesis remember all digits & literal periods, and then we match the rest of the string till end of line (implicit). And then the whole match gets replaced with the remembered digits & periods.

PS:跟进@Nemo 的建议:我们让 shell globbing 和先验知识处理一半的工作(只列出实际上看起来像 apache-tomcat 的东西)。当将 ls 的输出传送到另一个工具时, -1 没有实际意义,所以我们摆脱了它。sed 获取来自 ls 的值,将行首匹配到第一个 - 后跟一个数字,括号记住所有数字和文字句点,然后我们匹配字符串的其余部分直到行尾(隐式)。然后整个匹配被替换为记住的数字和句点。

回答by lxop

Pipe it through another cut:

通过另一个管道cut

ls -1 | grep '-tomcat-' | cut -f3 -d'-' | cut -f1-3 -d'.'

This will work as long as the versions all have three components. If the version is just 5.5, it won't work.

只要版本都具有三个组件,这将起作用。如果版本只有 5.5,则无法使用。

Another option would be just to use sed:

另一种选择是使用sed

ls -1 | grep '-tomcat-' | cut -f3 -d'-' | sed 's/.tar.gz\|.zip//'

This will remove .tar.gzor .zipfrom the strings.

这将从字符串中删除.tar.gz.zip

回答by matchew

ls -1 | awk -F. '/-tomcat-/ {print $1}'

ls -1 | awk -F. '/-tomcat-/ {print $1}'

a solution using awk. All previous answers would work just the same.

使用awk的解决方案。以前的所有答案都一样。

EDIT:

编辑:

I may have misunderstood, perhaps this is what youre after:

我可能误解了,也许这就是你所追求的:

ls -1 | awk -F\- '/tomcat/ {print substr($3,0,6)}'

ls -1 | awk -F\- '/tomcat/ {print substr($3,0,6)}'