Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

sed deleting file

by ddrew78 (Beadle)
on Dec 06, 2010 at 16:38 UTC ( #875648=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks, I have a file that looks like this:
What I need to do is get rid of the comma at the end. I tried using sed, like this:
system "sed 's/,)/)/' pbxiplp > pbxiplp1";
but, instead of giving me
, it just removes everything within the file. Any ideas? I've used 'sed' plenty in the past, but have never run into this before. The file will always only contain one line.

Replies are listed 'Best First'.
Re: sed deleting file
by johngg (Canon) on Dec 06, 2010 at 17:26 UTC

    Why use sed at all, you can do it all in Perl?

    knoppix@Microknoppix:~$ perl -Mstrict -wE ' > my $pbxiplp = <<EOF; > (,,) > EOF > print $pbxiplp; > > my $pbxiplp1; > open my $inFH, q{<}, \ $pbxiplp or die $!; > open my $outFH, q{>}, \ $pbxiplp1 or die $!; > while ( <$inFH> ) > { > s{ , (?= \) ) }{}; > print $outFH $_; > } > close $inFH or die $!; > close $outFH or die $!; > > print $pbxiplp1;' (,,) (, knoppix@Microknoppix:~$

    I'm using scalar refs. here as I'm doing this on the command line but it would work just as well with real files.

    I hope this is helpful.



Re: sed deleting file
by Fletch (Bishop) on Dec 06, 2010 at 17:21 UTC

    Calling out to sed from perl is pretty carrying-coal-to-Newcastle. Just open the old file, open the output, loop over the old lines using the same substitution, and print to the output file.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: sed deleting file
by Your Mother (Archbishop) on Dec 06, 2010 at 17:23 UTC

    This Perl works from the command line (add the -i if you test it and it seems safe). You can do similar tricks in a script with $^I.

    perl -p -e 's/,(?=\)\Z)//' [file list]
Re: sed deleting file
by roboticus (Chancellor) on Dec 06, 2010 at 16:43 UTC


    It looks like you left off the -e switch.

    $ cat foo (,,) $ sed -e 's/,)/)/' foo (,


    When your only tool is a hammer, all problems look like your thumb.

      tried it, still doesn't work :-(
Re: sed deleting file
by snoopy (Curate) on Dec 06, 2010 at 20:59 UTC
    Its always a good idea to do basic status checking of even the most trivial system call. There's always a chance of failure on a live system. For example:
    • incorrect path setup
    • exhaustion of resources such as physical disk
    • shell not available
    • permissions or process limits
    • missing utilities (although unlikely with sed)
    • many others ....
    system "sed 's/,)/)/' pbxiplp > pbxiplp1" and die "system call failed: $!";
    See also system.
Re: sed deleting file
by JavaFan (Canon) on Dec 06, 2010 at 19:42 UTC
    sed questions aren't Perl question. However, I cannot reproduce your problem:
    $ echo '(,,)' | sed 's/,)/)/' (, $
    I get that result both for BSD and GNU sed.
Re: sed deleting file
by choroba (Archbishop) on Dec 06, 2010 at 16:42 UTC
    You should not write to the same file you are reading. You can use sed's -i~ flag to change the file "in place", or save the output under a different name.
    Update: sorry, misread the question.
      I'm not...if you look, the file I'm writing to has a "1" at the end
Re: sed deleting file
by choroba (Archbishop) on Dec 06, 2010 at 16:55 UTC
    Try to modify it as follows:
    system "sed 's/,)/)/' pbxiplp > pbxiplp1" and die $?,$!;
      This one is going to be the end of me...still removes the content of the file/gives blank output
Re: sed deleting file
by cdarke (Prior) on Dec 07, 2010 at 13:05 UTC
    This is not sed(1) overwriting the output file, it is the shell. Redirection  > pbxiplp1 is a shell operation - if the file already exists it clobbers it (truncates to zero bytes). This is done by the shell before the sed(1) program is executed. By the time poor old sed gets to read the file the data is long gone.

    Use perl for this, or redirect to a different filename then rename it.
    Misread the question.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://875648]
Approved by planetscape
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2022-06-26 07:33 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (84 votes). Check out past polls.