MhnSkta
MhnSkta

Reputation: 83

shell script to transform a string to another form

i want to transform a string from one form to another, for example if input:

localhost -> ABC-NPCCache-Resource-Provisioning 2.3.1.1

output

ABC-NPCCache-Resource-Provisioning-2.3.1.1.tar.gz

i tried a lot but unable to find short way in shell script

Upvotes: 0

Views: 46

Answers (2)

Walter A
Walter A

Reputation: 20022

sed kan remember a matching string (a number of characters different from a space) and show it like this:

mystring='localhost -> ABC-NPCCache-Resource-Provisioning 2.3.1.1 '
echo "${mystring}" | sed -r 's/.*-> ([^ ]*) ([^ ]*).*/\1.\2.tar.gz/'
# result:
ABC-NPCCache-Resource-Provisioning.2.3.1.1.gz

Upvotes: 0

Kent
Kent

Reputation: 195209

this sed one-liner may give you a hand:

sed 's/.*-> //;s/ /-/;s/$/.tar.gz/'

with your example:

kent$  sed 's/.*-> //;s/ /-/;s/$/.tar.gz/'<<<'localhost -> ABC-NPCCache-Resource-Provisioning 2.3.1.1'    
ABC-NPCCache-Resource-Provisioning-2.3.1.1.tar.gz

or with awk:

awk -F' -> ' '{gsub(/ /,"-",$2);$0=$2".tar.gz"}7'

Upvotes: 1

Related Questions