使用 shell 命令提取版本

问题描述

我正在尝试使用诸如 grep、awk、sed、cut 之类的 shell 命令从下面提到的 URL 中提取最合适的版本

data.Age

我希望从 URL 中单独提取版本(带点的数字),其位置可能与上例中的一样。寻求建议。

预期输出:

https://abcd/efgh/1.1.3/hijkl/mnop    
https://abcd/efgh/hijkl/2.3.4.5/mnop    
https://abcd/3.4/efgh/hijkl/mnop

解决方法

您可以使用此grep

grep -Eo '[0-9]+(\.[0-9]+)+' file

1.1.3
2.3.4.5
3.4
,

使用 awk,在 GNU awk 中使用所示示例编写和测试。

awk 'match($0,/([0-9]+\.){1,}[0-9]+/){print substr($0,RSTART,RLENGTH)}' Input_file

说明:为以上添加详细说明。

awk '                               ##Starting awk program from here.
match($0,}[0-9]+/){   ##using match function to match regex of ([0-9]+\.){1,}[0-9]+ in current line. 
  print substr($0,RLENGTH)   ##Printing sub string of matched regex above,starting index is RSTART till value of RLENGTH here.
}
' Input_file                        ##Mentioning Input_file name here.
,

我会按照以下方式使用 GNU AWK,让 file.txt 内容成为

https://abcd/efgh/1.1.3/hijkl/mnop    
https://abcd/efgh/hijkl/2.3.4.5/mnop    
https://abcd/3.4/efgh/hijkl/mnop

然后

awk 'BEGIN{RS="[/\n]"}/^[.[:digit:]]+$/' file.txt

输出

1.1.3
2.3.4.5
3.4

说明:我将行分隔符 (RS) 指定为 / 或换行符 (\n) 然后只打印行(即 / 或换行符和 / 或 / 和换行符之间的部分)只包含 . 或数字 - 为了达到这种效果,我使用 ^$ 表示记录的开始和结束。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...