| Newsgroups |
gmane.editors.sed.user |
| Message-ID |
<[email protected]> |
On Sun, 5 Jul 2015 14:16:43 +0000 (UTC), "Scott Walters
[email protected] [sed-users]" <[email protected]> wrote:
> Hello,
> A theoretical question.echo abcd | sed 'p;p;p' prints the word
> "abcd" 4 times.Is there a simpler way to print a word N times ( N=large
> number) by NOT using the "p" command N-1 times.it has to be simple like
> we use in a regular for loop. thanksScott W
If you're trying to not hardcode the "4", it's not possible. That being
said, sed does have loops, so you can use a loop. But to know when you have
to break the loop, you have to somehow "save" how many times you have
looped so far. You can use the hold space to do that, so for example:
# print each input line 4 times
:loop
# switch to hold space
x
# if we have 4 "x", delete them and break
/x\{4\}/ {
s/.*//
x
d
}
# otherwise, append another "x"
s/$/x/
# switch to pattern space again
x
# print
p
# loop once more
b loop
--
D.