Remove word if it starts with - or --

Viewed 86

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?

5 Answers

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

Using sed

$ sed s'/^-[^ ]*\| --\?[^ ]*//g' input_file
apple house-roof computer-- orange number-43

You can also do it by converting each token to a line and then greping with a negated pattern.

For example:

xargs -n1 <input.txt | grep -v -E -e '^-[-]?[[:alnum:]]' | xargs

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 --

With your shown samples, please try following awk code. Simple explanation would be, going through each field value of each line and checking condition if field is NOT starting from either - OR -- then add its value to variable line at last print the value of line(awk variable, which is being nullified before for loop starts).

awk '
{
  line=""
  for(i=1;i<=NF;i++){
    if($i!~/^-[^-]/ && $i!~/^--[^-]/){
      line=(line?line OFS:"") $i
    }
  }
  print line
}
' Input_file
Related