Unix/Linux

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • make all lines the same length only with busybox commands

    5 answers - 1267 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Hi,
    I want to make all lines the same length.
    e.g.
    abc
    abcde
    ab
    abcdefg
    abc
    abc
    abc
    abcde
    ab
    abcdefg
    abc
    abc
    I only have a busybox with the following commands:
    BusyBox v1.01 (2005.11.20-20:04+0000) multi-call binary
    Usage: busybox [function] [arguments]
    or: [function] [arguments]
    BusyBox is a multi-call binary that combines many common Unix
    utilities into a single executable. Most people will create a
    link to busybox for each function they wish to use and BusyBox
    will act like whatever it was invoked as!
    Currently defined functions:
    [, ash, busybox, cat, chmod, clear, cp, cut, date, dd, df, dmesg,
    dos2unix, du, echo, env, expr, false, find, free, grep, halt,
    hostname, ifconfig, ifdown, ifup, inetd, init, insmod, ip, kill,
    killall, ln, loadkmap, login, ls, lsmod, md5sum, mkdir, more,
    mount, mv, nc, nslookup, passwd, pidof, ping, poweroff, ps, rdate,
    reboot, renice, rm, rmdir, rmmod, route, sed, sh, sleep, sync,
    tar, telnet, telnetd, test, time, touch, true, udhcpc, umount,
    uname, unix2dos, uptime, vi, wget, xargs, yes
    Do you know a way how to solve this problem?
    Thank you very much
    Dirk
  • No.1 | | 1059 bytes | |

    Mon, 06 Feb 2006 19:13:06 +0100, Dirk Joos wrote:
    Hi,

    I want to make all lines the same length.

    e.g.

    abc
    abcde
    ab
    abcdefg
    abc
    abc

    abc
    abcde
    ab
    abcdefg
    abc
    abc

    I only have a busybox with the following commands:
    []
    [, ash, busybox, cat, chmod, clear, cp, cut, date, dd, df, dmesg,
    dos2unix, du, echo, env, expr, false, find, free, grep, halt,
    hostname, ifconfig, ifdown, ifup, inetd, init, insmod, ip, kill,
    killall, ln, loadkmap, login, ls, lsmod, md5sum, mkdir, more,
    mount, mv, nc, nslookup, passwd, pidof, ping, poweroff, ps, rdate,
    reboot, renice, rm, rmdir, rmmod, route, sed, sh, sleep, sync,
    tar, telnet, telnetd, test, time, touch, true, udhcpc, umount,
    uname, unix2dos, uptime, vi, wget, xargs, yes
    []

    Does this work for you:

    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
  • No.2 | | 1707 bytes | |

    Stephane Chazelas schrieb:
    Mon, 06 Feb 2006 19:13:06 +0100, Dirk Joos wrote:
    >
    >>Hi,
    >>
    >>I want to make all lines the same length.
    >>
    >>e.g.
    >>
    >>abc
    >>abcde
    >>ab
    >>abcdefg
    >>abc
    >>abc
    >>


    >>
    >>abc
    >>abcde
    >>ab
    >>abcdefg
    >>abc
    >>abc
    >>
    >>I only have a busybox with the following commands:

    >

    []
    >
    >[, ash, busybox, cat, chmod, clear, cp, cut, date, dd, df, dmesg,
    >dos2unix, du, echo, env, expr, false, find, free, grep, halt,
    >hostname, ifconfig, ifdown, ifup, inetd, init, insmod, ip, kill,
    >killall, ln, loadkmap, login, ls, lsmod, md5sum, mkdir, more,
    >mount, mv, nc, nslookup, passwd, pidof, ping, poweroff, ps, rdate,
    >reboot, renice, rm, rmdir, rmmod, route, sed, sh, sleep, sync,
    >tar, telnet, telnetd, test, time, touch, true, udhcpc, umount,
    >uname, unix2dos, uptime, vi, wget, xargs, yes
    >

    []

    Does this work for you:

    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.

    Thanks

    Dirk
  • No.3 | | 2110 bytes | |

    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).
  • No.4 | | 463 bytes | |

    Dirk Joos wrote:
    Hi,

    I want to make all lines the same length.

    e.g.

    abc
    abcde
    ab
    abcdefg
    abc
    abc

    abc
    abcde
    ab
    abcdefg
    abc
    abc

    I only have a busybox with the following commands:

    Do you know a way how to solve this problem?

    you may also have interest to read an old post in this group:

    #ffbb90e4739d87e9

    Xicheng

    Thank you very much

    Dirk

  • No.5 | | 2337 bytes | |

    Stephane Chazelas schrieb:
    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

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


max 4000 letters.
Your nickname that display:
In order to stop the spam: 0 + 0 =
QUESTION ON "Unix/Linux"

EMSDN.COM