Discussion:
File - sed1line.txt
dave leonard daveleonard69@yahoo.com [sed-users]
2017-07-05 13:00:18 UTC
Permalink
Hello All. I have a one liner to print a word n times.
if we want to print the word hello 8 times in 8 lines, below are two methods
Method # 1 echo hello |sed 'p; x; s/^/ /; / \{8\}/d; x; s/^/\n/; D;' (by using hold space)
Method # 2 echo hello |sed ':a /\n/!s/$/\n/; P; s/$/ /; /\n \{8\}$/!ba; d;' (without using hold space)
if you like it pls add them to your list.
thanks
dave
sharma__r@hotmail.com [sed-users]
2017-07-06 07:14:18 UTC
Permalink
Method #3:

echo hello | sed -n ':a;/\n\{8\}/q;G;P;ba'


Here we setup a while loop in which the loop break-out condition is that 8 \n are found in the pattern space. If yes, means were done and we promptly quit. Since sed was invoked with -n option, this action of quitting shall generate no output. Otherwise when less than 8 \n were seen in the pattern space, we go ahead and append one more and then using the P command print the string hello and uconditionally branch back to the top of the loop.


[Non-text portions of this message have been removed]
dave leonard daveleonard69@yahoo.com [sed-users]
2017-07-06 09:20:20 UTC
Permalink
Hello all,
A slightly better variation of method 3.
echo hello | sed  ':a; /\n\{8\}/Q; G; P; ba'


No need to use the -n option at all.
thanksdave






[Non-text portions of this message have been removed]
sharma__r@hotmail.com [sed-users]
2017-07-08 12:30:18 UTC
Permalink
Note that Q is not a POSIX compliant feature.

You could replace the `Q` with `d` and retain the POSIX compliance:


echo hello | sed ':a; /\n\{8\}/d; G; P; ba'






[Non-text portions of this message have been removed]

Loading...