Home > OS >  shell script to filter input variables and cut as per the requirement
shell script to filter input variables and cut as per the requirement

Time:10-08

I have a requirement where 2 different repo names come such as app-na-repo1 and wcm-repo2. I need to cut the initials of both repo and assign the rest to say as domain. i.e.

repo=app-na-repo1-com-test or wcm-repo2-com-test

and with commands I need to filter it and cut it and assign to domain

such as final output should be any of this repo irrespective of the inputs

domain=repo1-com-test or repo2-com-test

tried several things, but not working out. Any help would be appreciated. We cut as per below, but how to setup the initial filter, tried if else but not working as can't filter

domain=$(echo "$repo" | cut -c8-)

domain=$(echo "$repo" | cut -c4-)

CodePudding user response:

$ echo "app-na-repo1/wcm-repo2" | sed -e 's/[^\/]*-//g'
repo1/repo2

CodePudding user response:

When you want to delete the two fixed strings, you can use sed as demonstrated in

for repo in 'app-na-repo1-com-test' 'wcm-repo2-com-test'; do
  printf "%-30s => " "${repo}"
  sed -r 's/^(app-na-)|(wcm-)//g' <<< "${repo}"
done

Result:

app-na-repo1-com-test          => repo1-com-test
wcm-repo2-com-test             => repo2-com-test
  • Related