0

I have modified the code found here: sed whole word search and replace

I have been trying to use the proper syntax \< and \> for the sed to match multiple terms in a file.

echo "Here Is My Example Testing Code" | sed -e "$(sed 's:\<.*\>:s/&//ig:' file.txt)"

However, I think, because it's looking into the file, it doesn't match the full word (only exact match) leaving some split words and single characters.

Does anyone know the proper syntax?

Example:

Input:

Here Is My Example Testing Code

File.txt:

example
test

Desired output:

Here Is My Code

DomainsFeatured
  • 1,426
  • 1
  • 21
  • 39
  • 1
    So you want to remove those words which are coming in File.txt from Input_file? Could you please let me know on same. – RavinderSingh13 Oct 10 '17 at 21:58
  • @RavinderSingh13 here you go: `echo "Here Is My Example Testing Code" | sed -e "$(sed 's:\<.*\>:s/&//ig:' file.txt)"` – DomainsFeatured Oct 10 '17 at 21:59
  • @RavinderSingh13, sorry I can't get it work with your code. Please try to stick with the question to provide the proper syntax for calling the file with the sed command I provided. – DomainsFeatured Oct 10 '17 at 22:27
  • could you please let me know if you down voted my answer? If yes, then please do let me know the reason(IMHO reason shouldn't be like my code is not working because that is written as per your specifications), person has right to know the reason. – RavinderSingh13 Oct 10 '17 at 22:40
  • Yes. I didn't get your code to work. And it is not written per my specifications. I specifically asked for SED. And, you asked me the question again after I just responded which is chatty. Did you down vote the question? – DomainsFeatured Oct 10 '17 at 22:44

2 Answers2

2

Modify your sed command as followed should extract what you want,

sed -e "$(sed 's:\<.*\>:s/&\\w*\\s//ig:' file.txt)"

Brief explanation,

  • \b matches the position between a word and a non-alphanumeric character. In this case, the pattern 'test' in file.txt would not match 'Testing'.
  • In this way, modify the searched pattern appended with \w* should work. \w actually matched [a-zA-Z0-9_]
  • And don't forget to eliminate the space behind each searched pattern, \s should be added.
CWLiu
  • 3,913
  • 1
  • 10
  • 14
0

Following awk could help you in same.

awk 'FNR==NR{a[$0]=$0;next} {for(i=1;i<=NF;i++){for(j in a){if(tolower($i)~ a[j]){$i=""}}}} 1' file.txt input
***OR***
awk '
FNR==NR{
  a[$0]=$0;
  next
}
{
for(i=1;i<=NF;i++){
  for(j in a){
    if(tolower($i)~ a[j]){
     $i=""}
}}}
1
' file.txt input

Output will be as follows.

Here Is My   Code

Also if your Input_file is always a single space delimited and you don't want unnecessary space as shown in above output, then you could use following.

awk 'FNR==NR{a[$0]=$0;next} {for(i=1;i<=NF;i++){for(j in a){if(tolower($i)~ a[j]){$i=""}}};gsub(/ +/," ")} 1' file.txt input
***OR***
awk '
FNR==NR{
a[$0]=$0;
next
}
{
for(i=1;i<=NF;i++){
 for(j in a){
   if(tolower($i)~ a[j]){
    $i=""}
}};
gsub(/ +/," ")
}
1
' file.txt input

Output will be as follows.

Here Is My Code
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93