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 8040947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:03:38+00:00 2026-06-05T04:03:38+00:00

Okay so I need to read in a file, go through each line and

  • 0

Okay so I need to read in a file, go through each line and find where there is the string ERROR. This is what I have so far:

open(LOGFILE, "input.txt") or die "Can't find file";

$title = <LOGFILE>;

$\=' ' ;
while (<>){
    foreach $title(split){
        while (/^ERROR/gm){
            print "ERROR in line $.\n";
        }
    }
}
close LOGFILE;

So the problem that I have is that it only looks at the first word of each line. So if the input is

boo far ERROR

It won’t register an error. any help would be greatly appreciated! I’m new to perl so please try and keeps things basic. Thanks!

  • 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-05T04:03:44+00:00Added an answer on June 5, 2026 at 4:03 am

    This is a more elegant approach, and I fixed the regex issue. ^ matched the start of a line.

    open(LOGFILE, "input.txt") or die "Can't find file";
    
    while(<LOGFILE>) {
       print "ERROR in line $.\n" if(/ERROR/);
    }
    close LOGFILE;
    

    Or how about from the command line:

    perl -n -e 'print "ERROR in line $.\n" if(/ERROR/);' input.txt
    

    -n implicitly loops for all lines of input
    -e executes a line of code

    To output to a file:

    perl -n -e 'print "ERROR in line $.\n" if(/ERROR/);' input.txt > output.txt
    

    While this is a good/simple example of using Perl, if you’re using a Unix shell, grep does what you want with no need for scripting (thanks to TLP in the OP comments):

    grep -n ERROR input.txt > output.txt
    

    This is actually prints the matching line itself, with its line number.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay so this is a bit complex but I need to loop through this
I need to read line by line from text file (log files from server)
Okay, I need help. I'm usually pretty good at SQL queries but this one
Okay, I have done a bit of searching online and found this thread, but
Okay, I have a page on and on this page I have an iframe.
I'm having some trouble using XmlReader to read an XML file. I can open
So I've been trying to read this particular .mat file into R. I don't
Okay so I've managed to read in a .txt file... now I'm trying to
Hello I have a homework assignment where I need to read two matrix .txt
okay so i'm working with visual studio 2010. I need to read out the

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.