sed: "Label too long" - Unix

This is a discussion on sed: "Label too long" - Unix ; I have a file that I need to process like this: 1. if the line starts with "#line", print is as is. 2. else if the line starts with "%% ", print everything after "%% ". 3. else print an ...

+ Reply to Thread
Results 1 to 12 of 12

Thread: sed: "Label too long"

  1. sed: "Label too long"

    I have a file that I need to process like this:

    1. if the line starts with "#line", print is as is.
    2. else if the line starts with "%% ", print everything after "%% ".
    3. else print an empty line.

    if my rusty perl serves me, it should be something like

    while (<>) {
    if (/^#line/) { print $_; }
    else if (/^%% /) { print s/^%% //; }
    else { print ""; }
    }

    how do I express this in sed?

    the following two versions work on some Unixes but not others
    (error "Label too long"):

    sed -e '/^#line/n;ta;:a;s/^%% //;tb;s/^.*\$\$//;:b'

    sed -e '/^#line/n;s/^%% //;t' -e 's/^.*\$\$//'

    I need a bullet-proof thing that will work everywhere.

    Thanks!

    PS. I do not know sed beyond "s///", the above was suggested by people
    who are apparently not much farther along than I am...

    --
    Sam Steingold (http://www.podval.org/~sds) running w2k


    A professor is someone who talks in someone else's sleep.

  2. Re: sed: "Label too long"



    Sam Steingold wrote:
    > I have a file that I need to process like this:
    >
    > 1. if the line starts with "#line", print is as is.
    > 2. else if the line starts with "%% ", print everything after "%% ".
    > 3. else print an empty line.
    >
    > if my rusty perl serves me, it should be something like
    >
    > while (<>) {
    > if (/^#line/) { print $_; }
    > else if (/^%% /) { print s/^%% //; }
    > else { print ""; }
    > }
    >
    > how do I express this in sed?


    Just curious - if you know how to do it in perl, why do you want to use
    sed? If you just don't have perl handy, why not use awk?

    Ed.

  3. Re: sed: "Label too long"

    > * Ed Morton [2005-07-27 14:13:26 -0500]:
    >
    > Sam Steingold wrote:
    >> I have a file that I need to process like this:
    >> 1. if the line starts with "#line", print is as is.
    >> 2. else if the line starts with "%% ", print everything after "%% ".
    >> 3. else print an empty line.
    >> if my rusty perl serves me, it should be something like
    >> while (<>) {
    >> if (/^#line/) { print $_; }
    >> else if (/^%% /) { print s/^%% //; }
    >> else { print ""; }
    >> }
    >> how do I express this in sed?

    >
    > Just curious - if you know how to do it in perl, why do you want to use
    > sed? If you just don't have perl handy, why not use awk?


    perl: portability.
    I cannot assume that every unix machine has a perl pre-installed on it.

    awk: I know absolutely nothing about it.
    is it present on every unix machine?
    at any rate, I would much rather fix the sed (I use sed in many places
    already, and I would rather not add a dependency on awk).

    thanks.


    --
    Sam Steingold (http://www.podval.org/~sds) running w2k


    Professionalism is being dispassionate about your work.

  4. Re: sed: "Label too long"

    In article , Sam Steingold wrote:
    >I have a file that I need to process like this:
    >
    >1. if the line starts with "#line", print is as is.
    >2. else if the line starts with "%% ", print everything after "%% ".
    >3. else print an empty line.
    >
    >if my rusty perl serves me, it should be something like
    >
    >while (<>) {
    > if (/^#line/) { print $_; }
    > else if (/^%% /) { print s/^%% //; }
    > else { print ""; }
    >}
    >
    >how do I express this in sed?


    Try this:

    sed -e '/^%% /{s///; n}' -e '/^#line /!s/.*//' myfile

    Explanation:

    /^%% /{s///; n}
    The "s" part: if line begins with "%% ", replace "%% " with "".
    The "n" part: print result and go to next line from myfile.

    /^#line/!s/.*//
    If line does not begin with "#line ", replace contents by "".

    Note: In C Shell and relatives, the "!" character is special,
    so you will have to protect it with a backslash, as in "\!".
    In Bourne shell and relative, a plain "!" should be OK.

    --
    Rouben Rostamian


  5. Re: sed: "Label too long"

    Sam Steingold wrote:
    > I have a file that I need to process like this:
    >
    > 1. if the line starts with "#line", print is as is.
    > 2. else if the line starts with "%% ", print everything after "%% ".
    > 3. else print an empty line.
    >
    > if my rusty perl serves me, it should be something like
    >
    > while (<>) {
    > if (/^#line/) { print $_; }
    > else if (/^%% /) { print s/^%% //; }
    > else { print ""; }
    > }
    >
    > how do I express this in sed?
    >
    > the following two versions work on some Unixes but not others
    > (error "Label too long"):
    >
    > sed -e '/^#line/n;ta;:a;s/^%% //;tb;s/^.*\$\$//;:b'
    >
    > sed -e '/^#line/n;s/^%% //;t' -e 's/^.*\$\$//'


    The better place to ask about scripting and general use of command line
    utilities is comp.unix.shell. Googling the archives of comp.unix.shell
    for "label too long" and sed turns up a few discussions on the issue.
    Apparently some implementations of sed consider everything after a
    colon as the label and limit the label length to 8 characters. So in
    your first expression on those implementations you have a label
    "a;s/^%% //;tb;s/^.*\$\$//;:b". Which is too long. The advice given
    is to define labels in their own -e option or to delimit with newlines
    rather than semicolons.

    -- ced

    >
    > I need a bullet-proof thing that will work everywhere.
    >
    > Thanks!
    >
    > PS. I do not know sed beyond "s///", the above was suggested by people
    > who are apparently not much farther along than I am...
    >



    --
    Chuck Dillon
    Senior Software Engineer
    NimbleGen Systems Inc.

  6. Re: sed: "Label too long"

    > * Rouben Rostamian [2005-07-27 23:46:05 +0000]:
    >
    > In article , Sam Steingold wrote:
    >>I have a file that I need to process like this:
    >>
    >>1. if the line starts with "#line", print is as is.
    >>2. else if the line starts with "%% ", print everything after "%% ".
    >>3. else print an empty line.
    >>
    >>if my rusty perl serves me, it should be something like
    >>
    >>while (<>) {
    >> if (/^#line/) { print $_; }
    >> else if (/^%% /) { print s/^%% //; }
    >> else { print ""; }
    >>}
    >>
    >>how do I express this in sed?

    >
    > Try this:
    >
    > sed -e '/^%% /{s///; n}' -e '/^#line /!s/.*//' myfile


    nope.
    it loses many lines.
    a fragment of the diff between output from your command and the correct
    output (produced by "sed -e '/^#line/n;s/^%% //;t' -e 's/^.*\$\$//'"):

    ====================================
    @@ -311,11 +311,11 @@


    #ifdef WIN32_NATIVE
    -
    + printf("#define WIN32_NATIVE\n");
    #endif
    -
    +#ifdef UNIX
    printf("#define UNIX\n");
    -
    +#endif


    ====================================
    original:
    ====================================
    %% #ifdef WIN32_NATIVE
    %% printf("#define WIN32_NATIVE\n");
    %% #endif
    %% #ifdef UNIX
    %% printf("#define UNIX\n");
    %% #endif
    ====================================


    > Explanation:
    >
    > /^%% /{s///; n}
    > The "s" part: if line begins with "%% ", replace "%% " with "".
    > The "n" part: print result and go to next line from myfile.
    >
    > /^#line/!s/.*//
    > If line does not begin with "#line ", replace contents by "".


    what does "; " mean?

    thanks!

    --
    Sam Steingold (http://www.podval.org/~sds) running w2k


    Just because you're paranoid doesn't mean they AREN'T after you.

  7. Re: sed: "Label too long"

    Sam Steingold wrote:

    > awk: I know absolutely nothing about it.
    > is it present on every unix machine?


    Awk is as omnipresent as sed and grep and the rest. There are multiple
    versions of awk though but all support the core original awk
    functionality. There is the original awk, nawk which is a new superset
    of awk and there is gawk the GNU version. If you write to the original
    awk your scripts should be portable. Similar scenarios exist for
    virtually all CL utilities since the GNU versions tend to have
    non-standard extensions.

    -- ced

    > at any rate, I would much rather fix the sed (I use sed in many places
    > already, and I would rather not add a dependency on awk).
    >
    > thanks.
    >
    >



    --
    Chuck Dillon
    Senior Software Engineer
    NimbleGen Systems Inc.

  8. Re: sed: "Label too long"

    In article , Sam Steingold wrote:
    >> * Rouben Rostamian [2005-07-27 23:46:05 +0000]:
    >>
    >> In article , Sam Steingold wrote:
    >>>I have a file that I need to process like this:
    >>>
    >>>1. if the line starts with "#line", print is as is.
    >>>2. else if the line starts with "%% ", print everything after "%% ".
    >>>3. else print an empty line.
    >>>
    >>>if my rusty perl serves me, it should be something like
    >>>
    >>>while (<>) {
    >>> if (/^#line/) { print $_; }
    >>> else if (/^%% /) { print s/^%% //; }
    >>> else { print ""; }
    >>>}
    >>>
    >>>how do I express this in sed?

    >>
    >> Try this:
    >>
    >> sed -e '/^%% /{s///; n}' -e '/^#line /!s/.*//' myfile

    >
    >nope.
    >it loses many lines.


    Oops, you are right. Sorry. Here is a corrected version:

    sed -e '/^%% /{s///p;d}' -e '/^#line /!s/.*//' myfile

    Explanation:
    The s///p part: replace the found string, i.e., "%% ", with ""
    and print the result
    The d part: delete the line that's being currently processed
    because we are done with it.

    >what does "; " mean?


    It's a command sepearator, as in:

    /RE/{ do this; do that }


    --
    Rouben Rostamian

  9. Re: sed: "Label too long"

    On 2005-07-28, Chuck Dillon wrote:
    > Sam Steingold wrote:
    >
    >> awk: I know absolutely nothing about it.
    >> is it present on every unix machine?

    >
    > Awk is as omnipresent as sed and grep and the rest. There are multiple
    > versions of awk though but all support the core original awk
    > functionality. There is the original awk, nawk which is a new superset
    > of awk and there is gawk the GNU version. If you write to the original
    > awk your scripts should be portable. Similar scenarios exist for
    > virtually all CL utilities since the GNU versions tend to have
    > non-standard extensions.


    It is safe to write for AWK as described in "The AWK Programming
    Language" (1988) which describes nawk (new awk), and is compatible
    with all versions of AWK except the old, pre-1987 awk.

    The new awk is as ubiquitous as sed and grep, and, except for a
    few systems, is available as awk. On Solaris, awk is the old awk,
    but a POSIX awk is also standard in the xpg4 directory, and I
    believe it also has nawk (under that name).

    >> at any rate, I would much rather fix the sed (I use sed in many places
    >> already, and I would rather not add a dependency on awk).


    For complicated scripts, you will find awk as easy, if not easier,
    to write, and it is much easier to understand, maintain and modify.

    --
    Chris F.A. Johnson
    ================================================== ================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress


  10. Re: sed: "Label too long"

    > * Rouben Rostamian [2005-07-28 14:39:42 +0000]:
    >
    >
    > sed -e '/^%% /{s///p;d}' -e '/^#line /!s/.*//' myfile


    this works with gnu sed, but not with sun sed:

    sed: command garbled: /^%% /{s///p;d}
    SunOS 5.8 Generic_117350-26 sun4u sparc SUNW,Sun-Blade-1000



    --
    Sam Steingold (http://www.podval.org/~sds) running w2k


    Whom computers would destroy, they must first drive mad.

  11. Re: sed: "Label too long"

    In article , Sam Steingold wrote:
    >> * Rouben Rostamian [2005-07-28 14:39:42 +0000]:
    >>
    >>
    >> sed -e '/^%% /{s///p;d}' -e '/^#line /!s/.*//' myfile

    >
    >this works with gnu sed, but not with sun sed:
    >
    >sed: command garbled: /^%% /{s///p;d}
    >SunOS 5.8 Generic_117350-26 sun4u sparc SUNW,Sun-Blade-1000


    Geez, you are a tough cutosmer ;-)
    This one ought to do it:

    sed -e '/^%% /{s///p;d;}' -e '/^#line /!s/.*//' myfile

    --
    Rouben Rostamian

  12. Re: sed: "Label too long"

    > * Rouben Rostamian [2005-07-28 15:25:39 +0000]:
    >
    > Geez, you are a tough cutosmer ;-)


    well, please complain to people who create the unix portability nightmare...

    > sed -e '/^%% /{s///p;d;}' -e '/^#line /!s/.*//' myfile


    indeed, it works on solaris, darwin and openbsd.

    thanks!

    --
    Sam Steingold (http://www.podval.org/~sds) running w2k


    If brute force does not work, you are not using enough.

+ Reply to Thread