1

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.

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

with grep if you have pcre option

$ echo '*****/XXXXXX>' | grep -oP '/\K[^>]+'
XXXXXX
  • /\K positive lookbehind / - not part of output
  • [^>]+ characters other than >

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.