I have one requirement.
I have one text file named as a.txt, which is having list of words –
GOOGLE FACEBBOK
Now I have one another file named as b.txt , which is having content as
Company name is google. Company name is facebook.
Like this n of lines are there with different different words.
Then I am writing script file –
FILENAME="a.txt" SCHEMA=$(cat $FILENAME) for L in $SCHEMA do echo "${L,,}" sed -i -E "s/.+/L&_/" b.txt done
So after running script the output file of b.txt file I am expecting is
Company name is google_ Company name is facebook_
But the output after running that script I am getting is –
Company name is google.__ Company name is facebook.__
And this output will be saved in b.txt file as I mentioned in sed command
Note – In a.txt I am having the list of Words which I want to replace and in b.txt file I am having paragraphs of line in which I am having words like google. , facebook. and so on.
So that’s why I am not able to give direct sed command for replacement.
I hope that you understand my requirement.
Thanks in advance!
Advertisement
Answer
You can use the following GNU sed
solution:
FILENAME="a.txt" while IFS= read -r L; do sed -i "s/($L)./1_/gI" b.txt done < $FILENAME
Or, the same without a loop as a single line (as used in anubhava’s answer):
sed -i -f <(printf 's/\(%s\)\./\1_/gIn' $(<"$FILENAME")) b.txt
With the script, you
while IFS= read -r L; do
– read the file line by line, each line being assigned toL
sed -i "s/($L)./1_/gI" b.txt
– replaces all occurrences ofL
(captured into Group 1 with the help of capturing(...)
parentheses) followed with.
(in a case insensitive way due toI
flag) inb.txt
with the same value as captured in Group 1 and_
appended to it.-f
allows passing a list of commands tosed
printf 's/\(%s\)\./\1_/gIn' $(<"$FILENAME")
creates a list ofsed
commands, in this case, it looks like
s/(GOOGLE)./1_/gI s/(FACEBOOK)./1_/gI