Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I replace a character with a defined number of different characters in sed?

Basic sed question:

This sed substitution works fine to replace a string of Ns with a Z:

cat test1 | sed -E "s/N{10}/Z/g"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But the reverse replaces a Z with the literal string "N{10}"

cat test2 | sed -e "s/Z/N{10}/g"

and returns something like this: AAAA**N{10}**AAAA

How can I replace "Z" with a string of 10 Ns? I know I can key in NNNNNNNNNN into the sed command, but I’m trying to understand the syntax. For some reason N{10} works as in the to-be-replaced but not as replace-with. I’ve tried to to this a variety of ways, but can’t find anything that works.

Any advice will be appreciated.

>Solution :

can do it:

perl -pe 's/Z/N x 10/e' file

With N x 10, you ask explicitly to repeat 1O times N

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading