使用 awk 为 C# 文件添加命名空间

发布于 2024-08-06 18:35:49 字数 1619 浏览 2 评论 0原文

我有几个 C# 类,其中名称空间未添加到文件中。示例文件如下所示 (Test.cs):

using System;
using System.IO;

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

如您所见,没有命名空间声明。我希望输出为:

using System;
using System.IO;

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

我想做的是编写一个快速脚本,基本上在类定义周围插入命名空间。我决定尝试一下 awk,所以我创建了一个 awk 文件,如下所示:

$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 "}";
}

对一个文件(如上面的文件)执行此操作后,它实际上没有执行任何操作(除了将“}”添加到文件底部之外) )。

这就是我理解我的正则表达式的使用方式:

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

该行必须以零个或多个空格开头,后跟单词“public”,后跟一个空格,后跟零个或多个空格(“partial”后跟一个空格),后跟单词“班级”。因此这些行应该匹配:

public class Test
public partial class Test

并且这些行不会匹配:

public void Test()
public string Test

但是,如果我对egrep使用类似的正则表达式:

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

我得到这个结果:

public partial class MyTestClass

阅读 awk 的手册页表明:

正则表达式是egrep中的扩展类型。

所以我不确定为什么我的 awk 脚本不起作用。任何帮助表示赞赏。也许有更好的方法来完成我想做的事情?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唐婉 2024-08-13 18:35:49

也许以不同的方式看待问题会有所帮助。不要尝试使用正则表达式来匹配之后/之前的行,而是尝试使用“段落”本身来获取整个“段落”。追加到该文件中,然后完成该文件。

    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 "}";
    }

编辑:最初我插入了一个“using”指令。他想要“命名空间”(和适当的语法):一个几乎相同的问题。固定的。

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.

如歌彻婉言 2024-08-13 18:35:49

这应该可以做到:

/^ *public (partial )*class / {trigger = 1 ; print "namespace MyNamespace"}
{print $0}
END {if(trigger) print "} //end of space"}

但你似乎对空白非常严格。我会放松一点,但我不相信自己的打字。

This should do it:

/^ *public (partial )*class / {trigger = 1 ; print "namespace MyNamespace"}
{print $0}
END {if(trigger) print "} //end of space"}

But you seems awfully strict about whitespace. I'd loosen it up a little, but then I don't trust my own typing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文