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?.

1

3 Answers 3

1

This one is good too

echo 'UNIX is good.' | sed 's/[^ ]*/&&&&/'
Sign up to request clarification or add additional context in comments.

1 Comment

If it's for n times? Instead just 4 times.
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.

Comments

0

a perl script:

#!/usr/local/bin/perl
use strict;
use warnings;
my $n= 4;
print 'UNIX'x$n . ' is good';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.