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

  • Home
  • SEARCH
  • 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 8231717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:35:21+00:00 2026-06-07T17:35:21+00:00

Okay so I want to delete the first occurrence of a string, let’s say,

  • 0

Okay so I want to delete the first occurrence of a string, let’s say, “dog”, in a file, and only the first occurrence. So if the file reads:

The dog was happy.
The dog liked to run.

The output would be

The was happy.
The dog liked to run.

I have the following code:

#!/usr/local/bin/perl

use warnings;
use strict;

my $file = $ARGV[0];
my $out = $ARGV[1];

open (IN, "<$file");
open (OUT, ">$out");
while(<IN>){
  s/dog/ /;
  print OUT $_;
}

close IN;
close OUT;

And it works, but instead of repacing just the first instance, it replaces all instances of the word. I thought I had to use the global modifier, g, to make it replace all instances? Why is it replacing all instances of the string I’m searching for? It’s probably some simple error I’m not seeing.

  • 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-07T17:35:23+00:00Added an answer on June 7, 2026 at 5:35 pm

    It replaces the first instance each time you evaluate the operator, and you evaluate the operator for each line, so you replace the first instances in each line. Had you used /g, you would have been replacing all instances in each line.

    my $seen;
    while (<>) {
       ++$seen if !$seen && s/dog//;
       print;
    }
    

    Or as a one-liner:

    perl -pe'++$seen if !$seen && s/dog//;' infile >outfile
    

    You can add the file handles back in, but the above accepts a more standard and more flexible usage. To get the same effect as with your code, you just have to use

    script.pl infile >outfile
    

    instead of

    script.pl infile outfile
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay. Say I have string '193' And I want to remove the last numbers
Okay, so what I want to do is to use a string as input
Okay so I want to make an application that launches other applications. However, the
Okay so here is what I want to do. I want to add a
Okay, so I want to have a generic method that can read data from
Okay so I have an idea on how I want to serve and cache
I just want to ask if is it okay that I use native PHP
Okay, so I have a UITableView that I want to apply themes to. I
okay...im really puzzled with this. I want to create a regular asp.net webforms website
Okay all, I have a form that I want users to fill out. When

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.