Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7933009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:11:42+00:00 2026-06-03T21:11:42+00:00

I cannot get this to work. #!/usr/bin/perl -w use strict; use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

  • 0

I cannot get this to work.

#!/usr/bin/perl -w
use strict;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
my $id='123456'; 
my $filetoopen = '/home/user/public/somefile.txt';

file contains:

123456
234564
364899
437373

So…
A bunch of other subs and code

if(-s $filetoopen){
     perl -n -i.bak -e "print unless /^$id$,/" $filetoopen;
}

I need to remove the line that matches $id from file $filetoopen

But, I don’t want script to “crash” if $id is not in $filetoopen either.

This is in a .pl scripts sub, not being run from command line.

I think I am close but, after reading for hours here, I had to resort to posting the question.

Will this even work in a script?

I tried TIE with success but, I need to know alternatively how to do this without TIE::FILE.

When I tried I got the error:

syntax error at mylearningcurve.pl line 456, near “bak -e “

Thanks for teaching this old dog…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:11:43+00:00Added an answer on June 3, 2026 at 9:11 pm

    First of all (this is not the cause of your problem) $, (aka $OUTPUT_FIELD_SEPARATOR) defaults to undef, I’m not sure why you are using it in the regex. I have a feeling the comma was a typo.

    It’s unclear if you are calling this from a shell script or from Perl?


    If from Perl, you should not call a nested Perl interpreter at all.

    If the file is small, slurp it in and print:

    use File::Slurp;
    my @lines = read_file($filename);
    write_file($filename, grep { ! /^$id$/ } @lines);
    

    If the file is large, read line by line as a filter.

    use File::Copy;
    move($filename, "$filename.old") or die "Can not rename: $!\n";
    open(my $fh_old, "<", "$filename.old") or die "Can not open $filename.old: $!\n";
    open(my $fh, ">", $filename) or die "Can not open $filename: $!\n";
    while my $line (<$fh_old>) {
        next if $line =~ /^id$/;
        print $fh $_;
    }
    close($fh_old);
    close($fh);
    

    If from a shell script, this worked for me:

    $ cat x1
    123456
    234564
    364899
    437373
    
    $ perl -n -i.bak -e "print unless /^$id$/" x1
    
    $ cat x1
    234564
    364899
    437373
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I cannot get this to work. I want to use str_to_date to convert a
I cannot get this to work, here is code that I found in another
I just cannot get this to work, would appreciate if someone can help. So
Ok, I've tried about near everything and I cannot get this to work. I
This is so simple but I cannot get it to work. I want to
Cannot get this to work. First time using variables passed into functions. Unchecking radio
I cannot get this to work: function formvalidation() { var SiteNum= document.getElementsByName(sitesinput)[0].value; var i=1;
This is my first attempt to inline Java code in Perl. We cannot use
When I have this PHP script. <?php exec('/usr/local/bin/mate hello.txt'); ?> It doesn't work on
I cannot get this to work. I have a MemoryStream object. This class has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.