Skip to main content
added 56 characters in body
Source Link
rewire
  • 111
  • 1
  • 6

My inputs are commands followed by an unrestricted number of single-letter options, e.g. command -abc. Neither the command nor the options take any arguments. My goal is to remove certain options.

Removing options b and c as an example, I can achieve this like this:

$ cmd='command -abc'
$ pattern='(.*) -(.*)'
$ [[ $cmd =~ $pattern ]]
$ echo "${BASH_REMATCH[1]} -${BASH_REMATCH[2]//[cb]}"
command -a

However, this only works in bash. How can I solve this problem in a compatible way, e.g. using sed and grep ?

My inputs are commands followed by an unrestricted number of single-letter options, e.g. command -abc. My goal is to remove certain options.

Removing options b and c as an example, I can achieve this like this:

$ cmd='command -abc'
$ pattern='(.*) -(.*)'
$ [[ $cmd =~ $pattern ]]
$ echo "${BASH_REMATCH[1]} -${BASH_REMATCH[2]//[cb]}"
command -a

However, this only works in bash. How can I solve this problem in a compatible way, e.g. using sed and grep ?

My inputs are commands followed by an unrestricted number of single-letter options, e.g. command -abc. Neither the command nor the options take any arguments. My goal is to remove certain options.

Removing options b and c as an example, I can achieve this like this:

$ cmd='command -abc'
$ pattern='(.*) -(.*)'
$ [[ $cmd =~ $pattern ]]
$ echo "${BASH_REMATCH[1]} -${BASH_REMATCH[2]//[cb]}"
command -a

However, this only works in bash. How can I solve this problem in a compatible way, e.g. using sed and grep ?

edited tags
Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k
Source Link
rewire
  • 111
  • 1
  • 6

Remove options from command

My inputs are commands followed by an unrestricted number of single-letter options, e.g. command -abc. My goal is to remove certain options.

Removing options b and c as an example, I can achieve this like this:

$ cmd='command -abc'
$ pattern='(.*) -(.*)'
$ [[ $cmd =~ $pattern ]]
$ echo "${BASH_REMATCH[1]} -${BASH_REMATCH[2]//[cb]}"
command -a

However, this only works in bash. How can I solve this problem in a compatible way, e.g. using sed and grep ?