ddrew78 has asked for the wisdom of the Perl Monks concerning the following question:

This one has me stumped. I'm starting with a file that looks something like this:
1.1.1.1 2.2.2.2 3.3.3.3
I need all of it to be on one line, with a space in between and some additional text, so I wrote this:
open(SESIP2, ">sesip2"); open(MYINPUTFILE, "sesip1"); while (<MYINPUTFILE>) { my($line) = $_; chomp($line); print SESIP2 "$line "; } close(MYINPUTFILE); close(SESIP2); open(SESIP3, ">sesip3"); open(MYINPUTFILE, "sesip2"); while (<MYINPUTFILE>) { my($line) = $_; print SESIP3 "dest \($line\)"; } close(MYINPUTFILE); close(SESIP3);
This gives me this: dest (1.1.1.1 2.2.2.2 3.3.3.3 ) So far so good. Now, I need to delete the space before ")", so i tried this simple one-liner:
system "sed 's/ )/)/' sesip3 > sesip2";
For some reason, this deletes the entire line, not just the space. Any ideas? I should probably mention that the IP addresses are stdin that get asked of the user earlier on. Everything else works like a charm, I just can't seem to get rid of that one space.

Replies are listed 'Best First'.
Re: why does 'sed' remove the entire line?
by JavaFan (Canon) on Jun 10, 2010 at 15:51 UTC
    You say you do the system command, but you aren't saying when. Considering you're opening several files (and without actually checking whether the open succeeds), it does matter.

    Your sed command seems to be correct - when running it from the command like it does what you think it should do.

    Of course, I would have written the entire thing in Perl:

    use 5.010; open my $in, "<", "sesip1" or die; open my $out, ">", "sesip2" or die; my @lines = <$in>; chomp @lines; { local $" = " "; say $out "dest (@lines)"; } close $out or die;
      That worked perfectly, thank you so much.
Re: why does 'sed' remove the entire line?
by jettero (Monsignor) on Jun 10, 2010 at 15:42 UTC
    What if you just used Perl? as long as you're in there, no reason at all to fork to sed: $line =~ s/\s+$//mg....
Re: why does 'sed' remove the entire line?
by ambrus (Abbot) on Jun 11, 2010 at 21:46 UTC

    I tried and I couldn't reproduce your problem.

    One thing I can imagine could go wrong is that your output file sesip2 does contain what it should but you didn't see its contents correctly (either because it has no newline and your prompt has overwritten it or because it has extra carriage returns), so try printing it with this command:

    cat -A sesip2 ; echo

    Update: also please make sure that your system "sed line is after all the close commands.

Re: why does 'sed' remove the entire line?
by Generoso (Prior) on Jun 10, 2010 at 15:52 UTC

    All you need is to get rid of the last blank before you use the concatenated IP's;

    open(SESIP2, ">sesip2"); open(MYINPUTFILE, "sesip1"); my $line = <MYINPUTFILE>; while (<MYINPUTFILE>) { my($line) = $_; chomp($line); print SESIP2 " $line"; } close(MYINPUTFILE); close(SESIP2); open(SESIP3, ">sesip3"); open(MYINPUTFILE, "sesip2"); while (<MYINPUTFILE>) { my($line) = $_; chomp($line); # this is the added line <---------- print SESIP3 "dest \($line\)"; } close(MYINPUTFILE); close(SESIP3);
      I tried that, but instead of having a space before the ")", this just put it after the "(" instead. But thanks for the help.