I am looking for a way to extract the string between "/" and "_" in the following examples. I tried this command but it doesn't work with special characters: sed -e 's//(.*\)_/\1/'
input:
Sometext/CPCT02010547T_sometext
out:
CPCT02010547T
I am looking for a way to extract the string between "/" and "_" in the following examples. I tried this command but it doesn't work with special characters: sed -e 's//(.*\)_/\1/'
input:
Sometext/CPCT02010547T_sometext
out:
CPCT02010547T
You can use
sed -n 's/.*\/\(.*\)_.*/\1/p'
sed -En 's~.*/([^_]*)_.*~\1~p'
The -n option suppresses the default line output and p prints the result of substitution.
The first pattern is a POSIX BRE regex:
.* - match any text\/ - matches a /\(.*\) - a capturing group (\1) in a POSIX BRE (first) pattern_ - a _ char.* - the rest of the string.The second regex is a POSIX ERE regex (note the -E option) and the ~ is used as a regex delimiter char to avoid escaping /. The only big difference between the first and second is the capturing group pattern, ([^_]*) matches any zero or more chars other than a _.