Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Odd string wrap (http://www.velocityreviews.com/forums/t893612-odd-string-wrap.html)

gbostock@excite.com 08-05-2005 02:44 PM

Odd string wrap
 
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


Paul Lalli 08-05-2005 03:26 PM

Re: Odd string wrap
 
gbostock@excite.com 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


gbostock@excite.com 08-05-2005 04:32 PM

Re: Odd string wrap
 

Paul Lalli wrote:
> gbostock@excite.com 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



Paul Lalli 08-05-2005 04:49 PM

Re: Odd string wrap
 
gbost...@excite.com 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-Dif...b/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


Dave Weaver 08-08-2005 08:04 AM

Re: Odd string wrap
 
On 5 Aug 2005 07:44:41 -0700, gbostock@excite.com <gbostock@excite.com> 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.

Big and Blue 08-10-2005 11:19 PM

Re: Odd string wrap
 
gbostock@excite.com 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.


All times are GMT. The time now is 07:58 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.