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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:27:50+00:00 2026-05-11T16:27:50+00:00

using grep, vim’s grep, or another unix shell command, I’d like to find the

  • 0

using grep, vim’s grep, or another unix shell command, I’d like to find the functions in a large cpp file that contain a specific word in their body.

In the files that I’m working with the word I’m looking for is on an indented line, the corresponding function header is the first line above the indented line that starts at position 0 and is not a ‘{‘.

For example searching for JOHN_DOE in the following code snippet

int foo ( int arg1 ) 
{
    /// code 
}
void bar ( std::string arg2  )
{
    /// code
    aFunctionCall( JOHN_DOE );
    /// more code
}

should give me

void bar ( std::string arg2  )

The algorithm that I hope to catch in grep/vim/unix shell scripts would probably best use the indentation and formatting assumptions, rather than attempting to parse C/C++.

Thanks for your suggestions.

  • 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-05-11T16:27:50+00:00Added an answer on May 11, 2026 at 4:27 pm

    As far as I know, this can’t be done. Here’s why:

    First, you have to search across lines. No problem, in vim adding a _ to a character class tells it to include new lines. so {_.*} would match everything between those brackets across multiple lines.

    So now you need to match whatever the pattern is for a function header(brittle even if you get it to work), then , and here’s the problem, whatever lines are between it and your search string, and finally match your search string. So you might have a regex like

    /^\(void \+\a\+ *(.*)\)\_.*JOHN_DOE
    

    But what happens is the first time vim finds a function header, it starts matching. It then matches every character until it finds JOHN_DOE. Which includes all the function headers in the file.

    So the problem is that, as far as I know, there’s no way to tell vim to match every character except for this regex pattern. And even if there was, a regex is not the tool for this job. It’s like opening a beer with a hammer. What we should do is write a simple script that gives you this info, and I have.

    fun! FindMyFunction(searchPattern, funcPattern)
      call search(a:searchPattern)
      let lineNumber = line(".")
      let lineNumber = lineNumber - 1
      "call setpos(".", [0,  lineNumber, 0, 0])
    
      let lineString = getline(lineNumber)
      while lineString !~ a:funcPattern
        let lineNumber = lineNumber - 1
        if lineNumber < 0
          echo "Function not found :/"
        endif
        let lineString = getline(lineNumber)
      endwhile
    
      echo lineString
    
    endfunction
    

    That should give you the result you want and it’s way easier to share, debug, and repurpose than a regular expression spit from the mouth of Cthulhu himself.

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

Sidebar

Ask A Question

Stats

  • Questions 439k
  • Answers 439k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you want to keep Height dynamic then you can't… May 15, 2026 at 4:53 pm
  • Editorial Team
    Editorial Team added an answer var gameOfLife = function() { } is a function expression,… May 15, 2026 at 4:53 pm
  • Editorial Team
    Editorial Team added an answer For user defined types there is QVariant::userType(). It works like… May 15, 2026 at 4:53 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.