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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:58:58+00:00 2026-06-14T08:58:58+00:00

If I perform this grep on my target file I get eg 275 as

  • 0

If I perform this grep on my target file I get eg 275 as result.

But I want to learn awk so tried this in awk:

 awk 'BEGIN { count=0 } /my pattern/ { count++ } END { print count }' myfile.log

And this prints the 275 as expected.

So getting ambitious I created an awk script like this:

BEGIN { 
   print "Log File Analysis";
   message=0;
   events=0;
}

{
  /message/  {  messages++;  }
  /event/    { events++;  }
}
END { 
  print "messages:\t" messages;
  print "events:\t" events;
}

I get a syntax error,

$ awk -f test_learn.awk test_log.log
awk: test_learn.awk:16:   /message/  {  messages++;  }
awk: test_learn.awk:16:              ^ syntax error

What am I doing wrong?

I am using awk from MinGW shell on windows 7.

  • 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-14T08:58:59+00:00Added an answer on June 14, 2026 at 8:58 am

    try

     awk 'BEGIN { count=0 }; /my pattern/{count++ }; END { print count }' myfile.log
    

    OR

    awk 'BEGIN { count=0};  { if ($0 ~ /my pattern/)  count++ }; END { print count };' myfile.log
    

    Better yet, as variables are initialized as zero by default, you don’t need the BEGIN block, so

    awk '/my pattern/{count++ }; END { print count };' myfile.log
    

    You can either have a default loop applied to all lines in a file, as in 2d example with the if, or you can have multiple blocks, “filtered” by pattern, as above, and in your edited addition.

    When doing one-liners have you have, some awks required the semi-colon to separate the BEGIN and END blocks from the main loop block.

    Edit

    Same Idea with your 2nd issue, and integrating Ed Morton’s improvments (thanks)

    /message/  {  messages++  }
    /event/    { events++  }
    
    END { 
     print "Log File Analysis"
     print "messages:\t" messages
      print "events:\t" events
    }
    

    IHTH

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

Sidebar

Related Questions

((List<string>)Session[answera]).Add(xle.InnerText); I need to perform this operation, but I get Object reference not set
I would like to perform something similar to this (ie get the sum of
I want to perform a simple query like this in my PHP, SELECT noslots
I read this question but the answer does not perform a refactoring of the
I am trying to perform this AJAX post but for some reason I am
I get the There isn't enought memory to perform this operation. Please close unneeded
In my current application in payment gateway, I want to perform this steps Transfer
I want to perform this.Controls.Add(MyObject) in the class definition of MyObject like public MyClass(container/*?*/
Basically I want to perform this check: var done = <?= $test_details['done_test'] ?>; if(typeof
When I perform this code: this.WindowState = FormWindowState.Minimized; _fullscreenForm.Show(); _fullscreenForm.Focus(); I get the current

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.