Rnj
Rnj

Reputation: 1189

Replacing all instances of string in one file with other

I will get an input file which may contain a lot of bad words. I want to replace every occurrence of bad word with "beep"

Here is my input file:

in.txt

May I have your attention, please?

Bad words file:

badwords.txt

Contains bad words

My bash script to do what I intent to do:

process.sh

for badWord in ${badWords[@]}; do
    outtext={outtext/badWord/beep}
done

The output:

$ ./process.sh 
May I have your attention, please?
=====================================================
May I havbeep yobbeepbeeppbeep attbeepntion, plbeepabeepbeep?

Why this is not replacing bad words correctly?

Upvotes: 4

Views: 74

Answers (1)

Rfroes87
Rfroes87

Reputation: 678

Some of the lines in cuss-words.txt contain spaces, so they will be broken at the for loop if you don't enclose the badWords array in double quotes.

Change this line:

for badWord in ${badWords[@]}; do

To this:

for badWord in "${badWords[@]}"; do

Upvotes: 2

Related Questions