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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:20:15+00:00 2026-05-11T22:20:15+00:00

I’d like to do a search for simple if statements in a collection of

  • 0

I’d like to do a search for simple if statements in a collection of C source files.

These are statements of the form:

if (condition)
    statement;

Any amount of white space or other sequences (e.g. “} else “) might appear on the same line before the if. Comments may appear between the “if (condition)” and “statement;”.

I want to exclude compound statements of the form:

if (condition)
{
    statement;
    statement;
}

I’ve tried each of the following in awk:

awk  '/if \(.*\)[^{]+;/ {print NR $0}' file.c    # (A) No results
awk  '/if \(.*\)[^{]+/ {print NR $0}' file.c    # (B)
awk  '/if \(.*\)/ {print NR $0}' file.c          # (C)

(B) and (C) give different results. Both include items I’m looking for and items I want to exclude. Part of the problem, obviously, is how to deal with patterns that span multiple lines.

Edge cases (badly formed comments, odd indenting or curly braces in odd places, etc.) can be ignored.

How can I accomplish this?

  • 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-11T22:20:15+00:00Added an answer on May 11, 2026 at 10:20 pm

    I’m not sure how you’d do this with a one liner (I’m sure you could by using sed’s ‘n’ command to read the next line, but it would be very complicated), so you probably want to use a script for this. How about:

    perl parse_if.pl file.c
    

    Where parse_if.pl contains:

    #!/usr/bin/perl -w
    
    my $line_number = 0;
    my $in_if = 0;
    my $if_line = "";
    # Scan through each line
    while(<>)
    {
        # Count the line number
        $line_number += 1;
        # If we're in an if block
        if ($in_if)
        {
            # Check for open braces (and ignore the rest of the if block
            # if there is one).
            if (/{/)
            {
                $in_if = 0;
            }
            # Check for semi-colons and report if present
            elsif (/;/)
            {
                print $if_line_number . ": " . $if_line;
                $in_if = 0;
            }
        }
        # If we're not in an if block, look for one and catch the end of the line
        elsif (/^[^#]*\b(?:if|else|while) \(.*\)(.*)/)
        {
            # Store the line contents
            $if_line = $_;
            $if_line_number = $line_number;
            # If the end of the line has a semicolon, report it
            if ($1 =~ ';')
            {
                print $if_line_number . ": " . $if_line;
            }
            # If the end of the line contains the opening brace, ignore this if
            elsif ($1 =~ '{')
            {
            }
            # Otherwise, read the following lines as they come in
            else
            {
                $in_if = 1;
            }
        }
    }
    

    I’m sure you could do something fairly easily in any other language (including awk) if you wanted to; I just thought that I could do it quickest in perl by way of an example.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.