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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:48:33+00:00 2026-05-12T15:48:33+00:00

I have several C# classes where the namespace wasn’t added to the file. A

  • 0

I have several C# classes where the namespace wasn’t added to the file. A sample file looks like this (Test.cs):

using System;
using System.IO;

public partial class MyTestClass
{
    protected void MyTestVoid()
    {
        // Magic happens here
    }
}

As you can see there is no namespace declaration. I want the output to be:

using System;
using System.IO;

namespace MyNamespace
{
    public partial class MyTestClass
    {
       protected void MyTestVoid()
       {
            // Magic happens here
       }
    }
}

What I am trying to do is write a quick script to basically insert the namespace around the class definition. I decided to try awk out and so I created an awk file like so:

$0 ~ "namespace MyNamespace"
{
    foundTrigger = 1;
}

!foundTrigger
{
    if ($0 ~ "/^[:space:]*public[:space:]{1}(partial[:space:])*class/")
    {
        print "namespace MyNamespace\n{\n" $0;
    }
    else
    {
        print $0;
    }
}

END
{
    if (!foundTrigger)
        print "}";
}

After executing this for a file (like the one above), it doesn’t really do anything (aside from adding the “}” to the bottom of the file).

This is how I understand my regex to be used:

^[:space:]*public[:space:]{1}(partial[:space:])*class

The line must start with zero or more spaces followed by the word “public” followed by exactly one space followed by zero or more (“partial” followed by a space) followed by the word “class”. Therefore these lines should match:

public class Test
public partial class Test

And these won’t match:

public void Test()
public string Test

However if I use a similar regex for egrep:

egrep "^[[:space:]]*public[[:space:]]{1}(partial[[:space:]])*class" *.cs

I get this result:

public partial class MyTestClass

Reading the man pages of awk shows that:

Regular expressions are the extended kind found in egrep.

So I am not sure why my awk script isn’t working. Any help is appreciated. Perhaps there is a better way to accomplish what I am trying to do?

  • 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-12T15:48:33+00:00Added an answer on May 12, 2026 at 3:48 pm

    Maybe it helps to look at the problem a little differently. Instead of trying to match the line after/before with a regex, try grabbing the entire using “paragraph” itself. Append to that, and finish the file.

        BEGIN { 
            RS = ""; # Read by "paragraphs".  
                     # Records are separated by blank lines. 
        }
        {
            if ( NR == 1 )
            {
                print $0,"\n\nnamespace MyNameSpace;\n{";
                RS = "\n";
            }
            else
            {
                print "\t", $0;  # For indenting
            }
        }
        END {
            print "}";
        }
    

    Edit: Originally I inserted a “using” directive. He wanted “namespace” (and appropriate syntax): an almost identical problem. Fixed.

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

Sidebar

Related Questions

I'm using the System.DirectoryServices.AccountManagement namespace classes to manage the membership of several groups. These
I have several classes with static const data members. I would like to know
I have several classes each containing several methods annotated with @Test . The classes
I have several classes, and want to work with their collections like with a
This is for a web project so i have several classes that inherit from
I am not sure if the title is accurate. I have several classes like
I have several small classes that are in a single file in /app/models, similar
I have several static classes in the namespace mySolution.Macros such as static class Indent{
I am using Autofac 2.4.5.724 under vb.net 2010. I have several classes without default
I have a namespace with several C# classes that I want to review with

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.