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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:18:24+00:00 2026-06-11T21:18:24+00:00

I am trying to create a file with all function/enum/struct/etc names from a source

  • 0

I am trying to create a file with all function/enum/struct/etc names from a source file. For that, I am at the moment trying to use sed to accomplish something like this:

(original file)

function add1 (int i) {
    return i+1;
}

(output of sed)

function add1 (int i) {
}

In other words, I want to remove the actual contents of the function’s body. I could so far not get it to work. Any suggestions?

EDIT: I tried something like this, with no success (for now I am trying to only make the lines on the function’s body blank):

sed '/{/,/}/ s/.*//'
  • 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-11T21:18:25+00:00Added an answer on June 11, 2026 at 9:18 pm

    Instead of sed, you could always use awk in per-character field mode (FS=""):

    awk 'BEGIN {
             RS = "\n" ;
             FS = "" ;
             d = 0 ;
         }
    
         {
             for (i=1; i<=NF; i++)
                 if ($i == "{") {
                     d++ ;
                     if (d == 1) printf "{\n"
                 } else
                 if ($i == "}") {
                     d-- ;
                     if (d == 0) printf "}"
                 } else
                 if (d == 0)
                     printf "%s", $i ;
             if (d == 0) printf "\n"
         }' INPUT-FILE(s)...
    

    The above will skip the contents of any paired curly braces, i.e. function and structure bodies, array initializations, and so on, and output the result to standard output. You can specify one or more files. (If you don’t specify any files, it’ll expect input from standard input.)

    As it is now, it will get confused about braces within quotes or comments. That could be fixed in the same way, but it does get quite complicated fast. This is just a hack to get you most of the way.

    I added the semicolons (;) so you can just stuff everything in the above snippet on one long command line.

    The logic of the script is very simple. It uses the empty field separator (FS), so that every character in input will be their own field. The BEGIN rule is run once before any input is processed, and sets this up. For developer information, I also initialize d = 0 although it is not necessary for awk since it assumes uninitialized variables to be empty or zero as appropriate. It will track the current brace depth for each input character.

    The second braced expression will be executed once per every record. Since I set RS = "\n", each line is a separate expression. Thus, it will be executed once per input line. Due to FS = "", each character on that line will be a separate field. There are NF fields in the record: $1, $2, .., $(NF-1), and $NF. The three-part if clause simply outputs outermost braces, and everything not within braces (i.e. when d == 0).

    It is possible to extend this awk scriptlet to encompass comments, strings, character constants (use \047 to refer to a single quote, unless you put the script into a separate file with #!/usr/bin/awk -f), and to process or ignore preprocessor macros.

    It does get a bit complicated, and you’ll end up with a couple of hundred lines of awk script, but it should be quite reliable and reasonably fast. The reason it is possible is because the tokenization rules in C in this particular case are easy to follow; I personally would use a full-blown C lexer (lexical analyzer or scanner) in all other use cases. And probably for this, too.

    If you want to use a full-blown C lexer, there are a number of them available freely on the net, but you’ll have to use a higher level language like C or C++. If you wish to handle all the corner cases, it’ll need to incorporate a C/C++ preprocessor, too, but those rules are easy (even with awk).

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

Sidebar

Related Questions

I am trying to create a PHP function that downloads images from a webpage
I'm trying to create function which removes all file and directories on webhosting excluding
Here's an idea. I'm trying to create file from PHP script. File may be
I am just trying to create a file with QProcess by the following source
I am trying to create a json file from a rake task using rabl.
i am trying to create an .htaccess file that will achieve the following create
I am trying to create a helper function to read a file and mock
I am trying to create an app that sends a video file to a
I'm trying to create a function that scans a folder on my Windows PC
I am trying to create a mex file that interfaces MATLAB with an external

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.