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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:47:03+00:00 2026-06-06T05:47:03+00:00

Hope the AWK gurus can provide a solution to my problem . I have

  • 0

Hope the AWK gurus can provide a solution to my problem .

I have a file that goes like this :

cat cat cat cat cat cat dog rat ate dog tit 
dog cat dog dog dog rat dog pat ate cat dog

I have to use AWK to extract the pattern between the first occuring c and a d .Starting from the first c a count should be kept on the number of c‘s and d‘s such that when the count matches , the part between the first c and the matched d shoud be ouput to a file including the number of the line in which the match for d occured .

In this particular example the match occurs on the seventh dog , therefore the output will have to be :

cat cat cat cat cat cat dog rat ate dog tit 
dog cat dog dog dog rat d

The match can go beyond just two lines ! The output can or cannot be inclusive of the c and the d .There exists all kinds of characters inclusive of the special ones in the text !
In order for the print to occur the count has to be matched .

Thanks in advance for the replies. Suggestions are always welcome .

EDIT : The capture of the pattern between c and d can be compromised as long as the condition is met and the line number of the exit d is obtained 🙂

  • 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-06T05:47:05+00:00Added an answer on June 6, 2026 at 5:47 am

    A few tips, without giving the full solution:

    By default, awk considers each line as a record. The default record separator is RS="\n".

    Depending on your version of awk, you may be able to set RS, the record separator, to a regex which matches either c or d. Then, for each record, you can check the RT variable, which will contain either c or d, depending on what has actually been matched. Starting from there, using a variable incremented on c, decremented on d you will be able to find the end of the match when it reaches 0.

    You can then use a variable that contains your match so far, and keep concatenating RT and the new record to it, until you’re done.

    If you need to know the line number of the end of the match, you can set RS to a regex that either matches c, d, as previously, but also add the possibility to match \n. And by maintaining another counter variable incremented every time RT tells you that \n has been matched, you’ll have your line number.

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

Sidebar

Related Questions

Hope to get solution to this problem. I have been stuck on it since
hope you can help me. I have html markup like this: <a href=error.htm class=button
Hope this is possible- Have a table like so: Car_ID Brand Model Year ----------------------------------
hope someone can help me. I have a function like <script language=JavaScript type=text/javascript> function
hope you can help me with this: I'm working with j_security_check and have this
Hope you can help me on this. I have a serial device, this device
Hope you are fine. I have a problem, if you can help me. #My
Hope I'm asking this correctly: I have a project Projects.Client I have my class
hope someone can help. I have two tables: Users -UserID -UserName UsersType -UserTypeID -UserID
Hope this is not a dupe. I would like to be able to do

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.