How to add characters in word and replace it using sed command in linux

saurabh704

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!

Wiktor Stribiżew

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_/gI\n' $(<"$FILENAME")) b.txt

With the script, you

  • while IFS= read -r L; do - read the file line by line, each line being assigned to L
  • sed -i "s/\($L\)\./\1_/gI" b.txt - replaces all occurrences of L (captured into Group 1 with the help of capturing \(...\) parentheses) followed with . (in a case insensitive way due to I flag) in b.txt with the same value as captured in Group 1 and _ appended to it.
  • -f allows passing a list of commands to sed
  • printf 's/\\(%s\\)\\./\\1_/gI\n' $(<"$FILENAME") creates a list of sed commands, in this case, it looks like
s/\(GOOGLE\)\./\1_/gI
s/\(FACEBOOK\)\./\1_/gI

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to replace word occurrence in ranges specified using sed command?

From Dev

How to replace a word inside a .DOCX file using Linux command line?

From Dev

Special characters to mass replace string using sed command

From Dev

how to replace $ value in a file using sed command?

From Dev

How can we remove text from start to some particular selected word using sed command in linux

From Dev

linux bash sed search and replace with sed characters

From Dev

Using sed to replace \ characters in file

From Dev

Replace word for blank using sed

From Dev

How do i search & replace using sed and not include a group of characters?

From Dev

How to replace only specific 20 characters using sed

From Dev

Using sed to find a word and replace another word

From Dev

How can I replace text after a specific word using sed?

From Dev

How can I replace text after a specific word using sed?

From Dev

fedora sed command replace special characters

From Dev

How to recursively replace characters with sed?

From Dev

Replace a line using sed command

From Dev

How do I replace non word characters with a dash using TSQL?

From Dev

sed replace special characters with new line on Linux

From Dev

UNIX: how to translate "/" when using search and replace command by sed

From Dev

How to replace specific row using sed or awk based with run command

From Dev

sed command to replace a string with special characters with another string with special characters

From Dev

Replace value within single quotes in a line that contains certain word using sed command

From Dev

Linux sed command replace regex string

From Dev

Linux sed command replace regex string

From Dev

SED Linux command to replace in exactly in a line

From Dev

How to replace a word in the for command in batch?

From Dev

Using sed to replace/remove accented Latin characters

From Dev

Using sed to replace characters between two patterns

From Dev

Using sed to replace strings containing special characters

Related Related

  1. 1

    How to replace word occurrence in ranges specified using sed command?

  2. 2

    How to replace a word inside a .DOCX file using Linux command line?

  3. 3

    Special characters to mass replace string using sed command

  4. 4

    how to replace $ value in a file using sed command?

  5. 5

    How can we remove text from start to some particular selected word using sed command in linux

  6. 6

    linux bash sed search and replace with sed characters

  7. 7

    Using sed to replace \ characters in file

  8. 8

    Replace word for blank using sed

  9. 9

    How do i search & replace using sed and not include a group of characters?

  10. 10

    How to replace only specific 20 characters using sed

  11. 11

    Using sed to find a word and replace another word

  12. 12

    How can I replace text after a specific word using sed?

  13. 13

    How can I replace text after a specific word using sed?

  14. 14

    fedora sed command replace special characters

  15. 15

    How to recursively replace characters with sed?

  16. 16

    Replace a line using sed command

  17. 17

    How do I replace non word characters with a dash using TSQL?

  18. 18

    sed replace special characters with new line on Linux

  19. 19

    UNIX: how to translate "/" when using search and replace command by sed

  20. 20

    How to replace specific row using sed or awk based with run command

  21. 21

    sed command to replace a string with special characters with another string with special characters

  22. 22

    Replace value within single quotes in a line that contains certain word using sed command

  23. 23

    Linux sed command replace regex string

  24. 24

    Linux sed command replace regex string

  25. 25

    SED Linux command to replace in exactly in a line

  26. 26

    How to replace a word in the for command in batch?

  27. 27

    Using sed to replace/remove accented Latin characters

  28. 28

    Using sed to replace characters between two patterns

  29. 29

    Using sed to replace strings containing special characters

HotTag

Archive