Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

sed deleting file

by ddrew78 (Beadle)
on Dec 06, 2010 at 16:38 UTC ( [id://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:
(3.3.3.3,4.4.4.4,)
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
(3.3.3.3,4.4.4.4)
, 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; > (3.3.3.3,4.4.4.4,) > 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;' (3.3.3.3,4.4.4.4,) (3.3.3.3,4.4.4.4) 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.

    Cheers,

    JohnGG

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 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 roboticus (Chancellor) on Dec 06, 2010 at 16:43 UTC

    ddrew78:

    It looks like you left off the -e switch.

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

    ...roboticus

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

      tried it, still doesn't work :-(
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 '(3.3.3.3,4.4.4.4,)' | sed 's/,)/)/' (3.3.3.3,4.4.4.4) $
    I get that result both for BSD and GNU sed.
Re: sed deleting file
by choroba (Cardinal) 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 (Cardinal) 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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-03-28 13:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found