0

I have a file which looks like:

file.txt

NNNP
PPZL
ZZNN

I would like to replace all the letters to "N" nd have an output like this with the same number of lines as the input: (my file here has 3 line but in reality, my file has thousands of lines)

NNNN
NNNN
NNNN

Indeed, I can just create a new file with this command:

printf %4s | tr " " "N" > file2.txt
yes "$(<file2.txt)" | head -n3

However, does anybody know if there is a more straightforward way to replace all the letters at once and maintain the same matrix of columns and rows?

1

The easiest way to achieve it is sed:

sed 's/[A-Z]/N/g' file.txt

(add -i if you want in-place file changes).

PS. Note, that the expression above is case sensitive. If you want case-insensitive replacement use something like this:

sed 's/[A-Z]/N/Ig' file.txt
2

To replace each alphabetic character by the letter N:

tr '[:alpha:]' N < file.txt > file2.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.