Re: make all lines the same length only with busybox commands
- From: Dirk Joos <Dirk@xxxxxxxxxxxxxx>
- Date: Tue, 14 Feb 2006 17:48:11 +0100
Stephane Chazelas schrieb:
On Mon, 06 Feb 2006 19:40:12 +0100, Dirk Joos wrote:
[...]
file=thefile
if
longest=`
sed 's/././g;x;G;s/^\(\.*\)\n\1./\1./;s/\n.*//;h;$!d' < "$file"
` &&
[ -n "$longest" ]
then
sed "s/\$/$longest/;s/\($longest\).*/\1/" < "$file"
fi
Wow, it is working. You are really fantastic. I have to go now. But when I'm coming back I'll try to understand this. And I'm sure I'll learn much by looking at it.
[...]
It's lacking some comments, I must confess.
The first sed computes the longest line in the file. It returns
the a string of as many dots as there are characters in the
longest line.
Basically, it first turns any character (except newlines) into
dots. The hold space contains the current longest line. We then
compare the length of the hold space with the length of the
length of the current line. If the current line is longer than
the hold space, then update the hold space.
To check whether the current line is longer than the hold space,
we check whether the hold space is contained into the current
line (remember every character has been changed to dots).
x;G
makes the pattern space look like:
<hold-space>\n<current-line>
s/^\(\.*\)\n\1./\1./
if it matches, replaces the content of the pattern space with
the current line.
s/\n.*//
for the case where the current line is not longer (the previous
substitution didn't match), remove what's after the \n, so
restore the <hold-space>.
Then, "h" stores that (possibly) updated hold space.
$!d prints the hold space on the last line.
The second sed appends that longest line of dots to every line
in the file, then, s/\($longest\).*/\1/ keeps only as many
leading characters of the lines as there are dots in $longest
(quite conveniently, "." happens to be the regexp operator that
matches one character).
Thank you very much for your great explanation. Now I understand it completely. And I really learned some new techniques which I can use for other problems.
Dirk
.
- References:
- make all lines the same length only with busybox commands
- From: Dirk Joos
- Re: make all lines the same length only with busybox commands
- From: Stephane Chazelas
- Re: make all lines the same length only with busybox commands
- From: Dirk Joos
- Re: make all lines the same length only with busybox commands
- From: Stephane Chazelas
- make all lines the same length only with busybox commands
- Prev by Date: Re: Embedding/nesting variables in ksh?
- Next by Date: will this script *always* work and pick out the file name?
- Previous by thread: Re: make all lines the same length only with busybox commands
- Next by thread: Re: make all lines the same length only with busybox commands
- Index(es):
Relevant Pages
|