0

I wanna remove a specific argument from a command like the one below,

mycommand --address=127.0.0.1 --file=/usr/local/somefile --port=8090 --key-file=/usr/local/server.key 

In this case, I wanna remove --file=/usr/local/somefile, how can I match the text from --file= to the next -- or to the N times occurrences of -- using sed.

Jeremy Wang
  • 1,495
  • 3
  • 13
  • 19
  • 3
    Post your efforts in to the question, even if they aren't solving your use-case. That way, we'll know the efforts you made. – Inian Aug 06 '18 at 08:23
  • check my answer here https://stackoverflow.com/questions/51382304/sed-to-remove-javascript-call-from-minified-html/51397354#51397354 – lojza Aug 06 '18 at 09:31

4 Answers4

1

If awk is an option, try this:

awk '{gsub(/--file=[^ ]+/, ""); print }'

From your input:

echo "mycommand --address=127.0.0.1 --file=/usr/local/somefile --port=8090 --key-file=/usr/local/server.key" \
| awk '{gsub(/--file=[^ ]+/, ""); print }'

Will output:

mycommand --address=127.0.0.1  --port=8090 --key-file=/usr/local/server.key
nbari
  • 25,603
  • 10
  • 76
  • 131
  • Try it on the original command. It will eat the `--port` option as well because `.*` is greedy. And on top of that, it won't work when `--file` is the last option. – PesaThe Aug 06 '18 at 09:14
  • @PesaThe thanks for the heads up, giving a try with `awk`. – nbari Aug 06 '18 at 12:04
0

Can you try the command -

1) this fulfills the first requirement - where it removes the required text from a pattern using Sed

echo "mycommand --address=127.0.0.1 --file=/usr/local/somefile --port=8090 --key-file=/usr/local/server.key" | sed -e "s/\([[:space:]]\)[[:space:]]*/\1/g" | sed -e "s/\(.*\)--file=[[:alnum:]\/]*\(.*\)/\1\2/"

mycommand --address=127.0.0.1  --port=8090 --key-file=/usr/local/server.key

Note: if it would have been Python, it is an easy go with Non-greedy regular expression which is unavailable in Sed

shellter
  • 36,525
  • 7
  • 83
  • 90
Jatish
  • 352
  • 1
  • 2
  • 10
  • For correct formatting, use 4 spaces at the front of lines that are code/output/error messages OR you can highlight code with mouse and then use the `{}` tool on the edit menu. Finally, maybe you can wrap you code to a second line, so it won't require scrolling to the right to view your solution. Good luck. – shellter Aug 06 '18 at 13:11
0

Bash has substitutions built in.

bash$ set -- mycommand --address=127.0.0.1 --file=/usr/local/somefile --port=8090 --key-file=/usr/local/server.key

bash$ echo "${@/--file=\/usr\/local\/somefile/}"
mycommand --address=127.0.0.1  --port=8090 --key-file=/usr/local/server.key

For a more robust solution, you probably want to convert the command line into an array instead.

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

This might work for you (GNU sed):

sed 's/--file=\S\+\s\?//' file

This removes the string beginning --file= and ending in one or non-space characters followed by an optional space.

sed -r 's/((--\S+\s?){3})--\S+\s?/\1/' file

This removes the fourth string beginning with -- followed by one or more non-space characters followed by a possible space. The fourth occurrence is found by grouping the first three such string and then another and replacing the match by the group.

The reverse of the above can be achieved by using:

sed '/\n/!s/--\S\+/\n&\n/g;/^--file=/P;D' file

This splits each line into string beginning with -- and printing the one(s) that begin --file=.

sed '/\n/!s/--\S\+/\n&\n/g;/^--/{x;s/^/x/;/^x\{3\}$/{x;P;x};x};D' file

This would extract the third option.

sed '/\n/!s/--\S\+/\n&\n/g;/^--/{x;s/^/x/;/^x\{2,3\}$/{x;P;x};x};D' file

This would extract the second and third options.

potong
  • 55,640
  • 6
  • 51
  • 83