2

I have a string like this:

austria rome italy venice london

I want to remove certain strings from above string:

rome venice

which should result in austria italy london.

I found sed but could not get it to work, also tried:

diff <(echo "string1") <(echo "string2") | grep "^<" | cut -c3-

but it also does not produce the desired output.

4 Answers 4

3

Use sed with extended regex to remove all unwanted words:

$ places='austria rome italy venice london'
$ echo $places|sed -E 's/austria|italy|london//g'
 rome  venice

Where:

  • -E is extended regex, to match multiple words via OR (|) operator
  • g matches all instances of found words

Update: Previous answer left leading whitespace that can be removed via:

$ echo $places|sed -E 's/austria|italy|london//g'|sed 's/^[ ]*//'
rome  venice

As Kristianmitk pointed out, double spaces created by a removed word's leading and ending spaces can be replaced by a single space:

$ echo $places|sed -E 's/austria|italy|london//g'|sed 's/^[ ]*//;s/  / /g'
rome venice

Alternatively, you can remove unwanted words and all trailing space after them:

$ echo $places|sed -E 's/austria *|italy *|london *//g'
rome venice
Sign up to request clarification or add additional context in comments.

8 Comments

It gives me sed: 1: "s/austria|italy|london//ig": bad flag in substitute command: 'i' Update: my bad, I had a typo.
i was for a case-insensitive search. I've tested it and it doesn't give that error, however, I've removed it from my answer as it isn't relevant to your question.
You are actually replacing the woulds by a whitespace. Add ;s/ / /g at the end of the regex to avoid that
you are totally right - should not type from my phone - down below in my answer its correct: s/ / /g - replacing double spaces by a single one. s/ //g would remove all spaces which would lead to a single long word (e.g romevenice)
@Kristianmitk Good point, thanks for spotting this, I've updated my answer :)
|
1

With sed a simple one-liner:

echo "austria rome italy venice london" | sed 's/rome//g;s/venice//g;s/  / /g'

Comments

0

You could convert your single line strings to one word per line, use grep -v, then convert back to a one-line string with paste -s. To avoid problems with word splitting, we first read the words into arrays:

$ str1='austria rome italy venice london'
$ str2='rome venice'
$ read -a arr1 <<< "$str1"
$ read -a arr2 <<< "$str2"
$ printf '%s\n' "${arr1[@]}" | grep -vf <(printf '%s\n' "${arr2[@]}") | paste -sd ' '
austria italy london

Alternatively, you could use tr:

tr ' ' '\n' <<< "$str1" | grep -vf <(tr ' ' '\n' <<< "$str2") | paste -sd ' '

Notice that after assigning to strings, this solves the general problem of "remove from one list of words all words contained in another list of words".

Comments

0

This isn't the most elegant solution as it stands, but may guide you in a more specific bash-only solution.

x=(austria rome italy venice london)
remove_from_x() { for i in "${!x[@]}"; do if [ "${x[$i]}" = "$1" ]; then unset x[$i]; fi; done }

remove_from_x rome
echo ${x[@]}     # -> austria italy venice london
remove_from_x venice
echo ${x[@]}     # -> austria italy london

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.