Find and Replace Various Word Within a File

4

I want to develop a script that will replace any words in file .

I know what I can do with the command (s) below:

$ tr 'ABC' '123' < arquivo.txt > novo-arquivo.txt

or

$ sed "s/ABC/123/g" arquivo.txt > novo-arquivo.txt

However, I do not just want to do the simple exchange of a "word" for "another", but rather make several changes of several " words " inside the file

For example:

  

I would like script to perform substitution in mass .. group / set words that is defined by the user itself. / p>

Suppose the file (document) contains the following words:

  • homen Home
  • sun Home
  • day

Then script should ask the question about the exchange, something like this:

1) - Type here, all words in which you want to replace them: man, sun, day

2) - Now, type in the same order as before, new ones to be inserted: woman, moon, night

  That is, automate change in general, instead of replacing only a single word, it can be more than one in different occurrence.

Conclusion

Then it would be enough to be defined by the user, to make the change in the file simultaneously (at one time). Change a set of words to other that the user can define.

    
asked by anonymous 05.10.2016 / 06:48

2 answers

3

To get the words to be replaced from the user input and put in a array , do so:

palavras=()
while IFS= read -r -p "Digite a palavra ([ENTER] para terminar): " linha; do
    [[ $linha ]] || break
    palavras+=("$linha")
done

Assuming you have arrays of words to replace and substitutes:

substituir=( "homem" "sol" "dia" )
substituto=( "mulher" "lua" "noite" )

Use a for loop to iterate over one of the arrays , and with sed you make the substitution:

for ((i=0; i<${#substituir[@]}; ++i)); do
    printf "Substituindo ${substituir[i]} por ${substituto[i]}...\n"

    sed -i "s/${substituir[i]}/${substituto[i]}/" foo.txt
done

See DEMO

Note : If you need to do the override globally, add the g modifier after the second delimiter.

You can also use Perl in one line:

perl -i -pe 's/homem/mulher/g; s/sol/lua/g; s/dia/noite/g' foo.txt 

The i option basically indicates that changes will be made to the file. p is used to iterate over the file, e indicates the code to be executed, in this case s/.../... . More information .

    
08.10.2016 / 00:03
3

Test (Requires Terminal / Console - Linux / Unix)

Create a text file with the following content: homen , day , sun p>

Now, open your console terminal, paste or type the command below:

for troca in 'cat teste.txt'; do echo "$(sed 's/homen/mulher/ ; s/dia/noite/ ; s/sol/lua/' teste.txt)" > teste.txt; $troca; done

Explaining

Loop for will pass the coordinates to the command cat "Read file" when invoked to variable $troca , and then "execute action for each line" via sed , at this point, note that I put sed in a subshell type involved in parentheses .

  

The advantage of this is that we do not need to create a new output file , already updating only the original file >.

Anyway - I booked the right to submit only one line and only one. It is already the core for purpose here.

    
07.10.2016 / 22:14