1

How to replace a string by number of times other/itself string?

e.g. UNIX is good. replace UNIX by 4 times O/p : UNIXUNIXUNIXUNIX is good.

How to do similar way n times?.

anilhr2learn
  • 31
  • 1
  • 3
  • 1
    Possible duplicate of [Multiplying strings in bash script](https://stackoverflow.com/questions/38868665/multiplying-strings-in-bash-script) – Benjamin W. Dec 12 '17 at 18:04

3 Answers3

1

This one is good too

echo 'UNIX is good.' | sed 's/[^ ]*/&&&&/'
ctac_
  • 2,413
  • 2
  • 7
  • 17
0

With this test file:

$ cat File
UNIX is good.

Try:

$ awk -v n=4 '{for (i=1;i<n;i++)sub(/(UNIX)+/, "&UNIX")} 1' File
UNIXUNIXUNIXUNIX is good.

Or:

$ s='UNIX is good.'
$ n=4; for ((i=1;i<n;i++)); do s=${s/UNIX/UNIXUNIX}; done; echo "$s"
UNIXUNIXUNIXUNIX is good.
John1024
  • 109,961
  • 14
  • 137
  • 171
0

a perl script:

#!/usr/local/bin/perl
use strict;
use warnings;
my $n= 4;
print 'UNIX'x$n . ' is good';
GAD3R
  • 4,317
  • 1
  • 23
  • 34