Re: make all lines the same length only with busybox commands



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
.



Relevant Pages

  • Re: make all lines the same length only with busybox commands
    ... the a string of as many dots as there are characters in the ... it first turns any character into ... makes the pattern space look like: ... The second sed appends that longest line of dots to every line ...
    (comp.unix.shell)
  • Equation Editor uses the wrong symbols
    ... dot over a character, it put an intersection symbol over the character ... Two dots become two intersection symbols, ... version of office (and equation editor) did not solve the problem? ...
    (microsoft.public.mac.office.word)
  • Re: I wanna implement the revolving slash while the process is executing
    ... Of course, simply growing the dots is easy, but I am not sure how to ... rather than writing the \r character each time, you'd only write it when you'd written as many periods as make up the line you want (presuambly, no more than there are characters wide on your console:)). ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Conditional Statements
    ... I'm not sure that strrchr() is going to be useful. ... single character; the OP wants to determine whether the name ends in a ... He can search for dots, ... remaining part of the string a) has no more dots in it and b) is the ...
    (comp.lang.c)
  • Re: How to split a single line into multiple lines of 10 characters each
    ... > after breaking each original input line into 10 character lines. ... > #pattern space: print through that first newline, ... then deletes the very first newline (not needed before the first ...
    (comp.unix.shell)