简体   繁体   English

使用grep regex选择第一个连字符

[英]Using grep regex to select to first hyphen

echo "Linux/DEB/mainbinary-0.1.20190424165331-0-armdef.deb" | grep -oE "([^\/]+$)"

This prints just the filename, without the directory structure, but I cannot manage to print just mainbinary from that string. 这仅打印文件名,没有目录结构,但是我无法从该字符串仅打印mainbinary。 Suggestions? 有什么建议吗?

echo "Linux/DEB/mainbinary-0.1.20190424165331-0-armdef.deb"  |grep -oP '.*/\K[^-]+'
mainbinary

This will scan till last / and ignore everything to its left and keep moving until - (excluding) 这将扫描到最后一个/并忽略其左侧的所有内容,并继续移动直到- (不包括)

And a sed alternative to PS.'s great grep -oP sed替代PS。最伟大grep -oP

echo "Linux/DEB/mainbinary-0.1.20190424165331-0-armdef.deb"  |sed -r 's#^.*/([^-]+).*#\1#'
mainbinary

With any awk in any shell on any UNIX machine: 在任何UNIX计算机上的任何shell中使用任何awk:

$ echo "Linux/DEB/mainbinary-0.1.20190424165331-0-armdef.deb" | awk -F'[/-]' '{print $3}'
mainbinary

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM