Replace “o” with “0” in large wordlist and save the original word

awksedtext processing

I have a large text file with words. Every word is on a single line (typical wordlist).

I want to replace all characters "o" with the number "0" in every word, and the new formed word to be pasted on the next line after the original word.

For example, if we have this two in the list:

dog
someone

After the manipulation, our file should looks like this:

dog
d0g
someone
s0me0ne

How this can be done with GNU tools like sed or awk?

Best Answer

You can use sed:

sed -i -e 'p' -e 's/o/0/g' file

Explanation:

  • -i: activates in-place editing of the file
  • -e 'p': just pastes the line
  • -e 's/o/0/g': replaces o with 0 and pastes the altered line

And if you want an awk solution:

awk '1;gsub("o", "0")' file >new_file
Related Question