I have below string:

"-owner -date 2017-10-10 -priority 20 -value xyz -outputLocation "

I want to ignore -priority 20 and -outputLocation options while parsing this parameters in bash script. any trick to do this using sed/awk/grep ?

note1: parameters don't have specific order. note2: -priority can be a number between 0-100 note3: -outputLocation . dir_name changes always with each run

closed as too broad by peterh, Archemar, Jeff Schaller, Romeo Ninov, G-Man Nov 23 '17 at 15:53

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


Method 1

Here I am replacing the contents “-priority 20 -outputLocation” with blank space.

for i in -priority 20 -outputLocation
  sed -i "s/"$i"//g" /tmp/l.txt

Method 2

Here I am nullifying the values.

awk '{$5="";$6="";$NF="";print $0}' l.txt
  • For loop seems good, but with each run values might change. e..g for next run priority might be 40 instead of 20. Also instead of -outputLocation there might be new parameter -outputDir. so how we can handle dynamic changes – Rocky86 Nov 22 '17 at 4:35
  • 1
    @Rock: Your question says “I want to ignore -priority 20”. If you want to ignore “-priority” followed by any number, you should say so in your question. – G-Man Nov 22 '17 at 4:57

Using argparse you can easily parse the parameters:

parser = argparse.ArgumentParser()
parser.add_argument('-owner', action='store_true')

args = parser.parse_args()

From there it's literally trivial to ignore some of the parameters.

  • can you please add some example using this one – Rocky86 Nov 22 '17 at 4:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.