| Newsgroups |
gmane.editors.sed.user |
| Message-ID |
<[email protected]> |
> - When P has run 4 times, PatSpace has \n\n\n\n, s substitutes \n #4, T
does not loop, sed exits.
After running 4 times, the PatSpace would have (the ^ $ are placeholders)
^abcd\n\n\n\n$
> $ N=4
> $ echo abcd | sed -e ":k x; /z\{$N\}/ d" -e 's/$/z/; x; p; bk'
I prefer the vertical form of sed one-liners as they are somewhat
self-documenting and appear like a regular programming language.
The horizontal sed one liners are hieroglyphics to the beginner;
and might also help in putting off the adoption of sed by prospective
users.
For example, the above when re-phrased in vertical style,
would appear as:
echo "abcd" |
sed -e "
...:loop
......x
........./.\{$N\}/d
........s/^/^/
......x
......p
...bloop
"
I have used the leading dots, because OTW the yahoo
mailer collapses the indenting.
Label names are chosen meaningfully so as to heighten
legibility, instead of uni-letter names.
From the layout we can make out that there is a do-while loop
and another indent is to imply that we are operating on the hold
space in that level.
While not completely explanatory, but it's incrementally more helpful.
-Rakesh
---In [email protected], <dgoldman@...> wrote :
That's very clever. Here is my best understanding how Rakesh's solution
works, please correct me if I didn't get it right.
$ N=4
$ echo abcd | sed -n ":k G; P; s/\n/&/$N; Tk"
abcd
abcd
abcd
abcd
- Each time through loop, P prints abcd (line #1 of PatSpace).
- For counter, G appends \n to PatSpace each time through loop.
- Seems like the order of G and P does not matter.
- Normally, s does not substitute, so T loops.
- When P has run 4 times, PatSpace has \n\n\n\n, s substitutes \n #4, T
does not loop, sed exits.
-------------------
I made a simpler version of Davide's solution, I think it still works,
he already explained how the solution works.
$ N=4
$ echo abcd | sed -e ":k x; /z\{$N\}/ d" -e 's/$/z/; x; p; bk'
abcd
abcd
abcd
abcd
---------------------
Daniel
>
>
> We can simplify the sed code a bit , since the pattern space
> gets loaded by input.
> Note: The $N even though it looks like a valid sed syntax, but
> here it is a shell variable N, storing the number of times
> to print the string "abcd". N > 0
>
>
> sed -ne "
>
> :loop
> G;P
> s/\n/&/$N
> Tloop
> "
>
>
>
>
>
> [Non-text portions of this message have been removed]
>
>
>
> ------------------------------------
> Posted by: sharma__r@... mailto:sharma__r@...
> ------------------------------------
>
[Non-text portions of this message have been removed]