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.
| [reply] [Watch: Dir/Any] [d/l] |
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.
| [reply] [Watch: Dir/Any] |
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]
| [reply] [Watch: Dir/Any] [d/l] |
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. | [reply] [Watch: Dir/Any] [d/l] |
Re: sed deleting file
by roboticus (Chancellor) on Dec 06, 2010 at 16:43 UTC
|
$ 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. | [reply] [Watch: Dir/Any] [d/l] [select] |
|
tried it, still doesn't work :-(
| [reply] [Watch: Dir/Any] |
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. | [reply] [Watch: Dir/Any] [d/l] |
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.
| [reply] [Watch: Dir/Any] [d/l] |
|
I'm not...if you look, the file I'm writing to has a "1" at the end
| [reply] [Watch: Dir/Any] |
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 $?,$!;
| [reply] [Watch: Dir/Any] [d/l] |
|
This one is going to be the end of me...still removes the content of the file/gives blank output
| [reply] [Watch: Dir/Any] |
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.
| [reply] [Watch: Dir/Any] [d/l] |