简体   繁体   中英

Regex to remove . and - in string using shell script

I have a string com.xyx.it.abc.sweta-test-parameter I need to convert it to com/xyz/it/abc/swetatestparameter

How can I achieve the output using Regex?

I need to replace. with / and remove - from string Below is the attempt I tried:

Package_Name="com.xyx.it.abc.sweta-test-parameter"
Package=${Package_Name//-//}

The output is:

com/xyx/it/abc/sweta-test-parameter

Can someone please help?

Appreciate all your help! Thanks in advance!

As suggested in the comments, sed will do the job. Please try replacing Package=${Package_Name//-//} with

Package=$(sed 's/\./\//g; s/-//g' <<< "$Package_Name")

You can do it directly in bash, but it takes two steps:

Package_Name="com.xyx.it.abc.sweta-test-parameter"

Package=${Package_Name//.//}    # Replace each "." with "/"
Package=${Package//-/}          # Replace each "-" with "" (i.e. delete them)

echo "$Package"                 # Prints "com/xyx/it/abc/swetatestparameter"

Note that this does not use regular expressions. The substitution mechanism supports glob patterns (filename-style wildcards), but this doesn't even use that, just plain strings.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM