Odd string wrap

G

gbostock

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
 
P

Paul Lalli

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
 
G

gbostock

Paul said:
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?
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
 
P

Paul Lalli

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?
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.

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
 
D

Dave Weaver

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.
 
B

Big and Blue

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top