0

I need to Remove String with special characters from a variable,

I have this string which is a path that I want to remove from a bigger path which I stored inside a variable.

So those are the parameters:

FULL_PATH=Server/.*/resources/schema/v12_55_6/.*/.*-dbSchemaDescriptor.xml,Server/.*/resources/SpringIOC/dataupgrader/v12_55_6/.*/.*-dataUpgrader.xml,Server/.*/java/com/company/mqm/dataupgrader/v12_55_6/.*/.*.java,Server/.*/resources/indexes/v12_55_6/.*.index,Server/.*/resources/schema/v12_55_7/.*/.*-dbSchemaDescriptor.xml,Server/.*/resources/SpringIOC/dataupgrader/v12_55_7/.*/.*-dataUpgrader.xml,Server/.*/java/com/company/mqm/dataupgrader/v12_55_7/.*/.*.java,Server/.*/resources/indexes/v12_55_7/.*.index,Server/.*/resources/schema/v12_55_8/.*/.*-dbSchemaDescriptor.xml,Server/.*/resources/SpringIOC/dataupgrader/v12_55_8/.*/.*-dataUpgrader.xml,Server/.*/java/com/company/mqm/dataupgrader/v12_55_8/.*/.*.java,Server/.*/resources/indexes/v12_55_8/.*.index

And this is the path(let's assume it's a string)that I want to remove from the variable:

REMOVE_PATH=Server/.*/resources/SpringIOC/dataupgrader/12_55_7/.*/.*-dataUpgrader\.xml,Server/.*/java/com/hp/mqm/dataupgrader/12_55_7,Server/.*/resources/indexes/12_55_7/.*\.index/.*/.*\.java

I tried to use shopt but it didn't do anything, This is the command I used:

shopt -s extglob;echo ${FULL_PATH//@($"{REMOVE_PATH}")}

*The FULL_PATH variable is something dynamic that can change so this is why I'm putting it inside a parameter.

Shahar Hamuzim Rajuan
  • 5,610
  • 9
  • 53
  • 91

1 Answers1

2

Simple examples to understand how pattern replacement works with shopt exglob:

shopt -s extglob

full_path=a/b,a/c,a/d,a/e,a/f
remove_path=a/d,a/c

echo "${full_path//@(${remove_path//,/|})?(,)}"
# a/b,a/e,a/f
# a/d and a/c where removed
# ?(,) at the end to remove comma after match if any


full_path=a/*/b,a/*/c,a/*/d,a/*/e,a/*/f
remove_path=a/*/d

echo "${full_path//@(${remove_path//,/|})?(,)}"
# a/*/e,a/*/f
# here a/*/b was removed because in a/*/d * matches every character including / so a/*/d longest match is a/*/b,a/*/c,a/*/d

Note that character with special meaning in pattern can be escaped with backslash \

full_path='a/*/b,a/*/c,a/*/d,a/*/e,a/*/f'
remove_path='a/\*/d,a/\*/c'

echo "${full_path//@(${remove_path//,/|})?(,)}"
# a/*/b,a/*/e,a/*/f

Otherwise to match every character except / for example the following pattern can be used *([^/]) : 0 or more characters except /

full_path='a/*/b,a/*/c,a/hello/d,a/*/e,a/*/f'
remove_path='a/*([^/])/d,a/\*/c'


echo "${full_path//@(${remove_path//,/|})?(,)}"
# a/*/b,a/*/e,a/*/f
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36
  • thanks it is working, but when trying to work with parameters inside double quotes it is failing, for example, remove_path="a/\*/d,a/\*/${param}" when param=c – Shahar Hamuzim Rajuan Dec 21 '17 at 12:37
  • it may not work because it's the second example where `*` matches everything (so `/` and `,`). Maybe you wanted `/\*/` to match a star litterally between slashes or `/*([^/,])/` to match any sequence except `/` and `,` between slashes – Nahuel Fouilleul Dec 21 '17 at 12:51
  • This is working - remove_path='a/*/d,a/*/"${param}"' taken from https://stackoverflow.com/questions/13799789/expansion-of-variable-inside-single-quotes-in-a-command-in-bash-shell-script – Shahar Hamuzim Rajuan Dec 21 '17 at 13:56
  • sure single quotes and double quotes are not the same, this is not what you wrote in the comment. But you still have the problem with `*` which is matching more that what you think – Nahuel Fouilleul Dec 21 '17 at 14:12