【发布时间】:2020-05-16 05:28:10
【问题描述】:
我在myfile 中有这样的行:
mount -t cifs //hostname/path/ /mount/path/ -o username='xxxx',password='xxxxx'
我需要根据条件“从// 开始直到下一个空格包括//”从中提取子字符串。
我无法解析位置,因为它在所有匹配的行中都不相同。
到目前为止,我已经使用 grep 的 perl 断言提取了子字符串,但结果没有返回 //。
我使用的这段代码是
cat myfile | grep " cifs " | grep -oP "(?<=/)[^\s]*" | grep -v ^/
输出:
hostname/path/
预期输出:
//hostname/path/
有没有办法通过修改 perl 正则表达式来获得所需的输出,也许是其他方法?
【问题讨论】:
-
试试
sed -En '/ cifs /{s,.*(//[^[:space:]]+).*,\1,p}' file,或grep -oE '//[^[:space:]]+' file
标签: regex bash shell perl grep