Home > Blockchain >  sed - output match string with regex
sed - output match string with regex

Time:03-06

I would like to extract a substring by regex and output the matched group.

In below example string,the expected output is the string after "package:" and end with ".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:

/data/app/~~6qMr1wvTvXFW_ceh1ptDHA==/com.sample.touch-tOazIbhNj63ME76BG6zrsA==/base.apk

CodePudding user response:

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
  • 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 (.*)
  • \1 - the replacement is Group 1 value.
  • Related