Parth Mody
Parth Mody

Reputation: 466

bash - Extract part of string

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

Upvotes: 2

Views: 228

Answers (6)

PravinY
PravinY

Reputation: 530

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

Upvotes: 1

Sundeep
Sundeep

Reputation: 23697

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

Upvotes: 1

pah
pah

Reputation: 4778

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

Upvotes: 1

razz
razz

Reputation: 10120

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'

Upvotes: 1

Nam Pham
Nam Pham

Reputation: 316

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

Upvotes: 2

chihung
chihung

Reputation: 1

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

Upvotes: 0

Related Questions