[英]sed - output match string with regex
I would like to extract a substring by regex and output the matched group.我想通过正则表达式提取 substring 和匹配组 output 。
In below example string,the expected output is the string after "package:" and end with ".apk".在下面的示例字符串中,预期的 output 是“package:”之后以“.apk”结尾的字符串。 below code works at beginning part, but the end part not work.下面的代码在开始部分有效,但结束部分无效。
echo "package:/data/app/~~6qMr1wvTvXFW_ceh1ptDHA==/com.sample.touch-tOazIbhNj63ME76BG6zrsA==/base.apk=com.sample.touch" |sed -E 's/package:([^ ]+apk)/\1/'
/data/app/~~6qMr1wvTvXFW_ceh1ptDHA==/com.sample.touch-tOazIbhNj63ME76BG6zrsA==/base.apk=com.sample.touch
The expected output:预期的 output:
/data/app/~~6qMr1wvTvXFW_ceh1ptDHA==/com.sample.touch-tOazIbhNj63ME76BG6zrsA==/base.apk
You need to use你需要使用
sed -E 's/package:([^ ]+apk).*/\1/'
# or
sed 's/package:\([^ ]*apk\).*/\1/'
See the online demo :请参阅在线演示:
#!/bin/bash
echo "package:/data/app/~~6qMr1wvTvXFW_ceh1ptDHA==/com.sample.touch-tOazIbhNj63ME76BG6zrsA==/base.apk=com.sample.touch" | \
sed -E 's/package:([^ ]+apk).*/\1/'
# => /data/app/~~6qMr1wvTvXFW_ceh1ptDHA==/com.sample.touch-tOazIbhNj63ME76BG6zrsA==/base.apk
Details :详情:
-E
- POSIX ERE syntax enabled -E
- 启用 POSIX ERE 语法package:([^ ]+apk).*
- package:
string, then one or more chars other than a space and then apk
substring captured into Group 1 ( ([^ ]+apk)
), and then the rest of the string ( .*
) package:([^ ]+apk).*
- package:
字符串,然后是除空格以外的一个或多个字符,然后是apk
substring 捕获到组 1 ( ([^ ]+apk)
),然后是字符串的 rest ( .*
)\1
- the replacement is Group 1 value. \1
- 替换为第 1 组值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.