2

I have two variables as follows :

sentence="name string,age int,address string,dob timestamp,job string"
ignore="age int,dob timestamp"

Basically I need to iterate through the comma separated variable $ignore and remove each word from the above varibale $sentence.

After performing this the output sentence should be as below:

echo $outputsentence
name string,address string,job string

Should I create an array for words to be ignored and iterate through it an perform a sed operation? Is there any other way around?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
user2349253
  • 109
  • 1
  • 10
  • 1
    Can you show us the shell code you have tried? – Jdamian Nov 03 '16 at 14:48
  • 1
    Please [edit] your question to show [what you have tried so far](http://whathaveyoutried.com). You should include a [mcve] of the code that you are having problems with, then we can try to help with the specific problem. You should also read [ask]. – Toby Speight Nov 03 '16 at 17:19

2 Answers2

1

This is a situation that requires sets: you want to know which members of set A are not present in set B.

For this we have a beautiful article Set Operations in the Unix Shell that describes all of them.

If you want to check the intersection of sets, say:

$ comm -12 <(tr ',' '\n' <<< "$sentence" | sort) <(tr ',' '\n' <<< "$ignore" | sort)
age int
dob timestamp

For complement, use comm -23:

$ comm -23 <(tr ',' '\n' <<< "$sentence" | sort) <(tr ',' '\n' <<< "$ignore" | sort)
address string
job string
name string

Note tr ',' '\n' <<< "$var" | sort just splits the ,-separated strings in slices. Then, <( ) is a process substitution.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
1

With GNU sed:

pattern=$(sed "s/,/|/g" <<< "$ignore")
outputsentence=$(sed -r 's/('"$pattern"'),*//g' <<< "$sentence")

The first sed command replace all , with an alternation operator | in the ignore list.

This result is used as a pattern in to remove the strings from $sentence.

SLePort
  • 15,211
  • 3
  • 34
  • 44