I just started using sed from doing regex. I wanted to extract XXXXXX
from *****/XXXXXX>
so I was following
sed -n "/^/*/(\S*\).>$/p"
If I do so I get following error
sed: 1: "/^/ /(\\S ).>$/p": invalid command code *
I am not sure what am I missing here.
Try:
$ echo '*****/XXXXXX>' | sed 's|.*/||; s|>.*||'
XXXXXX
The substitute command s|.*/||
removes everything up to the last /
in the string. The substitute command s|>.*||
removes everything from the first >
in the string that remains to the end of the line.
Or:
$ echo '*****/XXXXXX>' | sed -E 's|.*/(.*)>|\1|'
XXXXXX
The substitute command s|.*/(.*)>|\\1|
captures whatever is between the last /
and the last >
and saves it in group 1. That is then replaced with group 1, \\1
.
In my opinion awk performs better this task. Using -F you can use multiple delimiters such as "/" and ">":
echo "*****/XXXXXX>" | awk -F'/|>' '{print $1}'
Of course you could use sed , but it's more complicated to understand. First I'm removing the first part (delimited by "/") and after the second one (delimited by ">"):
echo "*****/XXXXXX>" | sed -e s/.*[/]// -e s/\>//
Both will bring the expected result: XXXXXX.
with grep
if you have pcre
option
$ echo '*****/XXXXXX>' | grep -oP '/\K[^>]+'
XXXXXX
/\\K
positive lookbehind /
- not part of output [^>]+
characters other than >
echo '*****/XXXXXX>' |sed 's/^.*\/\|>$//g'
XXXXXX
Start from start of the line, then proceed till lask /
ALSO find >
followed by EOL , if any of these found then replace it with blank.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.