Odd string wrap

Discussion in 'Perl Misc' started by gbostock@excite.com, Aug 5, 2005.

  1. Guest

    I'm trying to write a script that will do (UNIX) diffs on a set of
    files. I also want to pipe the results to grep -v to eliminate the
    trivial differences. The command gets quite long because of that and
    also because I use the full path name for the files. So the command
    looks like this:
    `diff /dir/dir1/dir2/dir3/file1 /dir/dir1/dir2/dir3/file2 | grep -v
    somestring | grep -v someotherstring ... >
    /dir/dir1/dir2/dir4/file.diff`

    Somehow that string is wrapped so that the UNIX command processor sees:

    diff /dir/dir1/dir2/dir3/file1
    /dir/dir1/dir2/dir3/file2
    | grep -v somestring | grep -v someotherstring ...

    so I get a message about the usage of diff, then it tries to execute
    file2 as if it were a shell script, then it complains about an
    unexpected | at the beginning of a command.

    I do not put any \n's or anything like that in the string. If I build
    that string with scalars into another scalar and I x that scalar in the
    perl debugger, it shows that string as 3 lines, broken up exactly in
    the way that causes the unix command processor to complain (as shown
    above). I looked for a system variable that might control that
    behavior, but couldn't find one.

    I get the same results if I use backticks or the system function call.

    Help?

    Gerald
    , Aug 5, 2005
    #1
    1. Advertising

  2. Paul Lalli Guest

    wrote:
    > I'm trying to write a script that will do (UNIX) diffs on a set of
    > files. I also want to pipe the results to grep -v to eliminate the
    > trivial differences. The command gets quite long because of that and
    > also because I use the full path name for the files. So the command
    > looks like this:
    > `diff /dir/dir1/dir2/dir3/file1 /dir/dir1/dir2/dir3/file2 | grep -v
    > somestring | grep -v someotherstring ... >
    > /dir/dir1/dir2/dir4/file.diff`


    Have you considered using Text::Diff and grep() instead of shelling all
    this out to external processes?

    > Somehow that string is wrapped so that the UNIX command processor sees:
    >
    > diff /dir/dir1/dir2/dir3/file1
    > /dir/dir1/dir2/dir3/file2
    > | grep -v somestring | grep -v someotherstring ...
    >
    > so I get a message about the usage of diff, then it tries to execute
    > file2 as if it were a shell script, then it complains about an
    > unexpected | at the beginning of a command.
    >
    > I do not put any \n's or anything like that in the string. If I build
    > that string with scalars into another scalar and I x that scalar in the
    > perl debugger, it shows that string as 3 lines, broken up exactly in
    > the way that causes the unix command processor to complain (as shown
    > above). I looked for a system variable that might control that
    > behavior, but couldn't find one.
    >
    > I get the same results if I use backticks or the system function call.
    >
    > Help?


    Uhm. Maybe you have an error on line 54, near "fctn1()".

    Seriously, though, how can we help you see what's wrong with your code
    if we can't see your code?

    Please post a short-but-complete script that demonstrates the error
    you're seeing. That is, a short script in which you assign scalar
    variables to strings without newlines, and print them out to see
    newlines magically appear. Then we can help you see what went wrong.

    Paul Lalli
    Paul Lalli, Aug 5, 2005
    #2
    1. Advertising

  3. Guest

    Paul Lalli wrote:
    > wrote:
    > > I'm trying to write a script that will do (UNIX) diffs on a set of
    > > files. I also want to pipe the results to grep -v to eliminate the
    > > trivial differences. The command gets quite long because of that and
    > > also because I use the full path name for the files. So the command
    > > looks like this:
    > > `diff /dir/dir1/dir2/dir3/file1 /dir/dir1/dir2/dir3/file2 | grep -v
    > > somestring | grep -v someotherstring ... >
    > > /dir/dir1/dir2/dir4/file.diff`

    >
    > Have you considered using Text::Diff and grep() instead of shelling all
    > this out to external processes?


    No I hadn't considered that because I was unfamiliar with Text::Diff. A
    search at http://perldoc.perl.org/index.html for Text::Diff didn't
    return any results either.

    The result I wanted was "external" so why wouldn't I shell it out?

    >
    > > Somehow that string is wrapped so that the UNIX command processor sees:
    > >
    > > diff /dir/dir1/dir2/dir3/file1
    > > /dir/dir1/dir2/dir3/file2
    > > | grep -v somestring | grep -v someotherstring ...
    > >
    > > so I get a message about the usage of diff, then it tries to execute
    > > file2 as if it were a shell script, then it complains about an
    > > unexpected | at the beginning of a command.
    > >
    > > I do not put any \n's or anything like that in the string. If I build
    > > that string with scalars into another scalar and I x that scalar in the
    > > perl debugger, it shows that string as 3 lines, broken up exactly in
    > > the way that causes the unix command processor to complain (as shown
    > > above). I looked for a system variable that might control that
    > > behavior, but couldn't find one.
    > >
    > > I get the same results if I use backticks or the system function call.
    > >
    > > Help?

    >
    > Uhm. Maybe you have an error on line 54, near "fctn1()".
    >
    > Seriously, though, how can we help you see what's wrong with your code
    > if we can't see your code?


    I can't show you the exact code because it is proprietary.

    >
    > Please post a short-but-complete script that demonstrates the error
    > you're seeing. That is, a short script in which you assign scalar
    > variables to strings without newlines, and print them out to see
    > newlines magically appear. Then we can help you see what went wrong.


    I tried to create a dummy program that would reproduce the problem, but
    I failed in the attempt. However it shed some light on the solution
    which I found. The problem is too embarassing to enumerate here.

    Thanks anyway,

    Gerald

    >
    > Paul Lalli
    , Aug 5, 2005
    #3
  4. Paul Lalli Guest

    wrote:
    > Paul Lalli wrote:
    > > Have you considered using Text::Diff and grep() instead of shelling all
    > > this out to external processes?

    >
    > No I hadn't considered that because I was unfamiliar with Text::Diff. A
    > search at http://perldoc.perl.org/index.html for Text::Diff didn't
    > return any results either.


    http://search.cpan.org/~rbs/Text-Diff-0.35/lib/Text/Diff.pm

    CPAN is the place you go to find "has anyone else already done this".

    > The result I wanted was "external" so why wouldn't I shell it out?


    I fail to see your line of reasoning. You have text files. You want
    to perform operations on them. You're writing a Perl script to do
    this. What is the logic in writing a Perl script that calls shell
    commands, as opposed to just doing it in Perl itself?

    > > Seriously, though, how can we help you see what's wrong with your code
    > > if we can't see your code?

    >
    > I can't show you the exact code because it is proprietary.


    I did not ask you to show us the exact code. See below.

    > > Please post a short-but-complete script that demonstrates the error
    > > you're seeing. That is, a short script in which you assign scalar
    > > variables to strings without newlines, and print them out to see
    > > newlines magically appear. Then we can help you see what went wrong.


    That is not the same as asking you to show us your proprietary code.

    > I tried to create a dummy program that would reproduce the problem, but
    > I failed in the attempt. However it shed some light on the solution
    > which I found. The problem is too embarassing to enumerate here.


    That is *exactly* the point of asking you to create a
    short-but-complete script - it almost always leads to the solution.
    This is why the Posting Guidelines ask that you do this.

    Paul Lalli
    Paul Lalli, Aug 5, 2005
    #4
  5. Dave Weaver Guest

    On 5 Aug 2005 07:44:41 -0700, <> wrote:
    > I'm trying to write a script that will do (UNIX) diffs on a set of
    > files. I also want to pipe the results to grep -v to eliminate the
    > trivial differences. The command gets quite long because of that and
    > also because I use the full path name for the files. So the command
    > looks like this:
    > `diff /dir/dir1/dir2/dir3/file1 /dir/dir1/dir2/dir3/file2 | grep -v
    > somestring | grep -v someotherstring ... >
    > /dir/dir1/dir2/dir4/file.diff`
    >
    > Somehow that string is wrapped so that the UNIX command processor sees:
    >
    > diff /dir/dir1/dir2/dir3/file1
    > /dir/dir1/dir2/dir3/file2
    > | grep -v somestring | grep -v someotherstring ...
    >


    You fail to provide enough information to determine the actual
    problem, but my spidey sense tells me that your filenames contain a
    "\n" at the end.

    If you are building up this command from a series of parts, check
    *exactly* what each part contains, e.g.
    print "[$filename]\n";

    If you see:
    [/dir/dir1/dir2/dir3/file1
    ]

    then you have a spurious "\n"

    Otherwise, follow the posting guidelines for this group by posting a
    short but complete program that will demonstrate the problem.
    Dave Weaver, Aug 8, 2005
    #5
  6. Big and Blue Guest

    wrote:
    >
    > Somehow that string is wrapped so that the UNIX command processor sees:
    >
    > diff /dir/dir1/dir2/dir3/file1
    > /dir/dir1/dir2/dir3/file2
    > | grep -v somestring | grep -v someotherstring ...


    Are you getting the file names by reading from a file?

    Have you forgotten to chomp()?

    Why not post the actual code so we don't have to guess?


    --
    Just because I've written it doesn't mean that
    either you or I have to believe it.
    Big and Blue, Aug 11, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Tozer

    Text string must not wrap

    Brian Tozer, Oct 22, 2003, in forum: HTML
    Replies:
    4
    Views:
    452
    Mark Parnell
    Oct 23, 2003
  2. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Replies:
    33
    Views:
    1,069
    Richard Heathfield
    Feb 18, 2007
  3. Aaron Fude

    To wrap or not to wrap?

    Aaron Fude, May 8, 2008, in forum: Java
    Replies:
    12
    Views:
    686
    Chronic Philharmonic
    May 10, 2008
  4. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 22, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    221
    Art Werschulz
    Sep 22, 2003
  5. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 24, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    233
    Anno Siegel
    Sep 25, 2003
Loading...

Share This Page