2

I have a string something like this

xsd:import schemaLocation="AppointmentManagementService.xsd6.xsd" namespace=

I want to extract the following from it :

AppointmentManagementService.xsd6.xsd

I have tried using regex, bash and sed with no success. Can someone please help me out with this?

The regex that I used was this :

/AppointmentManagementService.xsd\d{1,2}.xsd/g
0

6 Answers 6

2

Your string is:

nampt@nampt-desktop:$ cat 1
xsd:import schemaLocation="AppointmentManagementService.xsd6.xsd" namespace=

Try with awk:

cat 1 | awk -F "\"" '{print $2}'

Output:

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

Comments

1

sed doesn't recognize \d, use [0-9] or [[:digit:]] instead:

sed 's/^.*schemaLocation="\([^"]\+[[:digit:]]\{1,2\}\.xsd\)".*$/\1/g'
## or
sed 's/^.*schemaLocation="\([^"]\+[0-9]\{1,2\}\.xsd\)".*$/\1/g'

Comments

1

You can use bash native regex matching:

$ in='xsd:import schemaLocation="AppointmentManagementService.xsd6.xsd" namespace='

$ if [[ $in =~ \"(.+)\" ]]; then echo "${BASH_REMATCH[1]}"; fi

Output:

AppointmentManagementService.xsd6.xsd

Based on your example, if you want to grant, at least, 1 or, at most, 2 digits in the .xsd... component, you can fine tune the regex with:

$ if [[ $in =~ \"(AppointmentManagementService.xsd[0-9]{1,2}.xsd)\" ]]; then echo "${BASH_REMATCH[1]}"; fi

Comments

1

using PCRE in GNU grep

grep -oP 'schemaLocation="\K.*?(?=")'

this will output pattern matched between schemaLocation=" and very next occurrence of "

Reference:

https://unix.stackexchange.com/a/13472/109046

Comments

1

Also we can use 'cut' command for this purpose,

[root@code]# echo "xsd:import schemaLocation=\"AppointmentManagementService.xsd6.xsd\" namespace=" | cut -d\" -f 2 AppointmentManagementService.xsd6.xsd

Comments

0
s='xsd:import schemaLocation="AppointmentManagementService.xsd6.xsd" namespace='
echo $s | sed 's/.*schemaLocation="\(.*\)" namespace=.*/\1/'

1 Comment

Although this answer might possibly be correct, I'm recommending deletion as it has no explanation.

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.