Skip to main content
added 4 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

You get different results on the two systems because the GNU system uses a regular expression library that was extended to include some symbols from the type of regular expressions called PCRE (Perl Compatible Regular Expressions). These symbols, like \s and \w, are not standard and not supported with sed on all systems (typically not outside of GNU systems).

You seem to want to output the last part of a line after the last - (dash) if it starts with the substring deploy or something-deploy.

You can do that with standard sed using

sed '/^deploy/ s/.*-//'

or with

sed '/^something-deploy/ s/.*-//'

These sed expressions trigger the substitution s/.*-// if the preceding regular expression matches the current input line. Thi substitution removes everything up to and including the last dash on the line.

You can combine the triggering expression with the substitution in this case, if you want:

sed 's/^deploy.*-//'
sed 's/^something-deploy.*-//'

If your data is not static strings outputted by echo (as in the question) but some value in a variable, then it would be more efficient to let the shell do this rather than calling an external program:

string=deploy-api-production

if [[ $string == deploy-* ]]]]; then
    printf '%s\n' "${string##*-}"
fi

You will use a variable assignment rather than printf if you want to hold on to the modified value, as in new=${string##*-}.

The ${variable##pattern} expansion will remove the longest prefix string from $variable that matches the shell pattern pattern.

You get different results on the two systems because the GNU system uses a regular expression library that was extended to include some symbols from the type of regular expressions called PCRE (Perl Compatible Regular Expressions). These symbols, like \s and \w, are not standard and not supported with sed on all systems (typically outside of GNU systems).

You seem to want to output the last part of a line after the last - (dash) if it starts with the substring deploy or something-deploy.

You can do that with standard sed using

sed '/^deploy/ s/.*-//'

or with

sed '/^something-deploy/ s/.*-//'

These sed expressions trigger the substitution s/.*-// if the preceding regular expression matches the current input line. Thi substitution removes everything up to and including the last dash on the line.

If your data is not static strings outputted by echo (as in the question) but some value in a variable, then it would be more efficient to let the shell do this rather than calling an external program:

string=deploy-api-production

if [[ $string == deploy-* ]]
    printf '%s\n' "${string##*-}"
fi

You will use a variable assignment rather than printf if you want to hold on to the modified value, as in new=${string##*-}.

The ${variable##pattern} expansion will remove the longest prefix string from $variable that matches the shell pattern pattern.

You get different results on the two systems because the GNU system uses a regular expression library that was extended to include some symbols from the type of regular expressions called PCRE (Perl Compatible Regular Expressions). These symbols, like \s and \w, are not standard and not supported with sed on all systems (typically not outside of GNU systems).

You seem to want to output the last part of a line after the last - (dash) if it starts with the substring deploy or something-deploy.

You can do that with standard sed using

sed '/^deploy/ s/.*-//'

or with

sed '/^something-deploy/ s/.*-//'

These sed expressions trigger the substitution s/.*-// if the preceding regular expression matches the current input line. Thi substitution removes everything up to and including the last dash on the line.

You can combine the triggering expression with the substitution in this case, if you want:

sed 's/^deploy.*-//'
sed 's/^something-deploy.*-//'

If your data is not static strings outputted by echo (as in the question) but some value in a variable, then it would be more efficient to let the shell do this rather than calling an external program:

string=deploy-api-production

if [[ $string == deploy-* ]]; then
    printf '%s\n' "${string##*-}"
fi

You will use a variable assignment rather than printf if you want to hold on to the modified value, as in new=${string##*-}.

The ${variable##pattern} expansion will remove the longest prefix string from $variable that matches the shell pattern pattern.

Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

You get different results on the two systems because the GNU system uses a regular expression library that was extended to include some symbols from the type of regular expressions called PCRE (Perl Compatible Regular Expressions). These symbols, like \s and \w, are not standard and not supported with sed on all systems (typically outside of GNU systems).

You seem to want to output the last part of a line after the last - (dash) if it starts with the substring deploy or something-deploy.

You can do that with standard sed using

sed '/^deploy/ s/.*-//'

or with

sed '/^something-deploy/ s/.*-//'

These sed expressions trigger the substitution s/.*-// if the preceding regular expression matches the current input line. Thi substitution removes everything up to and including the last dash on the line.

If your data is not static strings outputted by echo (as in the question) but some value in a variable, then it would be more efficient to let the shell do this rather than calling an external program:

string=deploy-api-production

if [[ $string == deploy-* ]]
    printf '%s\n' "${string##*-}"
fi

You will use a variable assignment rather than printf if you want to hold on to the modified value, as in new=${string##*-}.

The ${variable##pattern} expansion will remove the longest prefix string from $variable that matches the shell pattern pattern.