What I want to do is in the title.
Input:
apple -berry --sun house-roof computer-- orange number-43
Desired output:
apple house-roof computer-- orange number-43
In other words, only when a word starts with -
or --
it should be removed. The closest I've got is:
echo "apple -berry --sun house-roof computer-- orange number-43" | sed -r 's/(-|--)\w //g'
apple house computer-- orange number
Which is far from what I want. Could be solved with other tools? Cut?
CodePudding user response:
You can use
sed -E 's/(^|[[:space:]] )-{1,2}[^[:space:]] //g'
Details:
-E
enables the POSIX ERE regex syntax(^|[[:space:]] )-{1,2}[^[:space:]]
- matches(^|[[:space:]] )
- start of string or one or more whitespaces-{1,2}
- one or two hyphens[^[:space:]]
- one or more non-whitespace chars.
See the online demo:
#!/bin/bash
s='apple -berry --sun house-roof computer-- orange number-43'
sed -E 's/(^|[[:space:]] )-{1,2}[^[:space:]] //g' <<< "$s"
Output:
apple house-roof computer-- orange number-43
CodePudding user response:
You can also do it by converting each token to a line and then grep
ing with a negated pattern.
For example:
xargs -n1 <input.txt | grep -v -E -e '^-[-]?[[:alnum:]]' | xargs
CodePudding user response:
Using sed
$ sed s'/^-[^ ]*\| --\?[^ ]*//g' input_file
apple house-roof computer-- orange number-43
CodePudding user response:
plain bash:
input="apple -berry --sun house-roof computer-- orange number-43"
read -ra words <<< "$input"
ok_words=()
for word in "${words[@]}"; do
[[ $word == @(-|--)* ]] || ok_words =("$word")
done
echo "${ok_words[*]}"
apple house-roof computer-- orange number-43
@(-|--)
is an extended pattern matching either -
or --