1

I want to replace a normal PATERN1 by a repeating pattern (PATERN2), in my case repeating 'N' 500 times.

Is it possible to do it simply with sed?

sed 's/PATERN1/N{500times}/g'

Cheers,

Ricardo

Ricardo Guerreiro
  • 497
  • 1
  • 4
  • 17
  • 1
    Possible duplicate of https://stackoverflow.com/questions/15072875/insert-with-sed-n-repeated-characters – Corentin Limier Dec 06 '18 at 14:27
  • Thank you, I hadn't found that before! I don't understand if it's possible with sed, but there is a similar solution there with perl: perl -pe 's/PATERN1/"N"x500 . ""/e' – Ricardo Guerreiro Dec 06 '18 at 14:53

1 Answers1

2

Using sed you can do this:

s="foo abc PATTERN foo bar PATTERN"

sed "s/PATT[^[:blank:]]*/$(printf '%.0bN' {1..10})/g" <<< "$s"

foo abc NNNNNNNNNN foo bar NNNNNNNNNN

Here printf '%.0bN' {1..10} will return a string of N repeated 10 times.

anubhava
  • 761,203
  • 64
  • 569
  • 643