C# 使用AWK或SED复制行并最终创建方法包装器

C# 使用AWK或SED复制行并最终创建方法包装器,c#,perl,macros,sed,awk,C#,Perl,Macros,Sed,Awk,对于这个简单的问题,在stackoverflow上还没有找到解决方案。有些人对此有所回答,但我是awk和sed的新手 我想在输入代码文件中为我的方法创建包装器。它有数百条线路,具有以下两个特点: public int GetImageType(ref fitsfile fptr, ref int imgtype, ref int status); public int fiwyhjl(ref fileFormat fptr, int axisx, ref int imagetyp

对于这个简单的问题,在stackoverflow上还没有找到解决方案。有些人对此有所回答,但我是awk和sed的新手

我想在输入代码文件中为我的方法创建包装器。它有数百条线路,具有以下两个特点:

   public int GetImageType(ref fitsfile fptr, ref int imgtype, ref int status);


   public int fiwyhjl(ref fileFormat fptr, int axisx, ref int imagetype, ref int axisy, ref long axisx, ref int status);
我想将其转换为:

 public int GetImageType(ref fitsfile fptr, ref int imgtype, ref int status)
 {
      return NativeMethods.GetImageType(ref fptr, ref imgtype, ref status);
 }


 public int fiwyhjl(ref fileFormat fptr, int axisx, ref int imagetype, ref int axisy, ref long axisx, ref int status)
 {
      return NativeMethods.fiwyhjl(ref fptr, axisx, ref imagetype, ref axisy, ref axisx, ref status);
 }
我能用awk或sed做些什么?作为第一步,我只想知道如何复制以“public”开头的每一行。在这种情况下,结果如下所示:

   public int GetImageType(ref fitsfile fptr, ref int imgtype, ref int status);
   public int GetImageType(ref fitsfile fptr, ref int imgtype, ref int status);


   public int fiwyhjl(ref fileFormat fptr, int axisx, ref int imagetype, ref int axisy, ref long axisx, ref int status);
   public int fiwyhjl(ref fileFormat fptr, int axisx, ref int imagetype, ref int axisy, ref long axisx, ref int status);
然后,我可以根据需要手动添加/修改其余部分。
谢谢

我不知道
sed
awk
,但是使用
perl
应该很容易。有关详细信息,请参阅。您可以将以下内容加倍:

s/^\s*public.*$/\0\r\n\0/ ;
您可以通过以下方式转换您的声明:

s/(public int GetImageType\(ref fitsfile fptr, ref int imgtype, ref int status\));/\1 { return NativeMethods.\1 ; }/ ;

[我的perl fu最近有点生锈,所以我可能有点走火入魔]。

试试这个perl one liner

perl -pe 'print m/(.*);/; s/public int //; s/ref \S+/ref/g; s/(.*);/ {\n\treturn NativeMethods.$1;\n}/' input.txt
input.txt包含

public int GetImageType(ref fitsfile fptr, ref int imgtype, ref int status);
public int fiwyhjl(ref fileFormat fptr, int axisx, ref int imagetype, ref int axisy, ref long axisx, ref int status);
将产生以下结果,

public int GetImageType(ref fitsfile fptr, ref int imgtype, ref int status)
{
        return NativeMethods.GetImageType(ref fptr, ref imgtype, ref status);
}
public int fiwyhjl(ref fileFormat fptr, int axisx, ref int imagetype, ref int axisy, ref long axisx, ref int status)
{
        return NativeMethods.fiwyhjl(ref fptr, int axisx, ref imagetype, ref axisy, ref axisx, ref status);
}

这是AWK版本

awk '{ header = gensub(/(.*);/,"\\1","g"); print header; body = gensub(/public \w+ (.*);/,"{\n\tNativeMethods.\\1;\n}","g"); gsub(/ref \w+/,"ref", body); print body}' input.txt
会产生,

public int GetImageType(ref fitsfile fptr, ref int imgtype, ref int status)
{
        NativeMethods.GetImageType(ref fptr, ref imgtype, ref status);
}
public int fiwyhjl(ref fileFormat fptr, int axisx, ref int imagetype, ref int axisy, ref long axisx, ref int status)
{
        NativeMethods.fiwyhjl(ref fptr, int axisx, ref imagetype, ref axisy, ref axisx, ref status);
}

sed
脚本:

sed  '/^public int/{s_;$__;p;s_\([(,] *\)[^ ]* \([^ ]*\)\([,)]\)_\1\2\3_g;s_\(ref\) [^ ]*_\1_g;s_public int _{\n    return NativeMethods._;s_$_;\n}_;p;d}'
将改变:

public int GetImageType(ref fitsfile fptr, ref int imgtype, ref int status);

public int fiwyhjl(ref fileFormat fptr, int axisx, ref int imagetype, ref int axisy, ref long axisx, ref int status);
致:

(注意,
int-axisx
在最后一行被正确更改为仅
axisx
。)

这是公认的丑陋,但可能比awk解决方案略短,但比perl更长。它有多强大,是否能满足OP的需求,我不知道。
int-axisx
axisx
的转换约占三分之一,包含在:

    s_\([(,] *\)[^ ]* \([^ ]*\)\([,)]\)_\1\2\3_g;

删除两个代币中的第一个。

我对AWK不太了解,但我认为这应该有效:

awk '{a=$0;sub(/public int /,"return NativeMethods.",a);gsub(/ref \S+/,"ref",a)};{if ($1 !~ /public/) ORS=RS;else ORS="\n"" {""\n""\t"a"\n"" }""\n\n"}1' file
这可能适用于您(GNU-sed):


将以
public…
开头的行复制到保留空间。重新排列当前行以适应附加的数据,将此数据附加到复制的行,然后用结果替换当前行。

Hi,感谢您的响应。我在Windows8上用草莓Perl(64位)5.18.1.1-64位进行了尝试。在EOF at-e第1行之前的任何地方,我都收到了错误“找不到字符串终止符”。对不起,我没有早点提到Win8。当然,这可能会有所不同。谢谢使用双引号,
perl-pe“print m/(.*);/;s/public int/;s/ref\s+/ref/g;s/(.*);/{\n\t返回NativeMethods.$1;\n}/”input.txt
感谢您的响应。对于新手的后续问题,我很抱歉。我应该在Win8/with gawk的命令行中包含这个。你知道为gawk转换的字符串吗?当我尝试使用gawk运行时,在单引号和双引号处出现错误。再次抱歉/谢谢。
awk '{a=$0;sub(/public int /,"return NativeMethods.",a);gsub(/ref \S+/,"ref",a)};{if ($1 !~ /public/) ORS=RS;else ORS="\n"" {""\n""\t"a"\n"" }""\n\n"}1' file
sed -r '/^(\s*)public\s+\S+\s+(.*)/{h;s//\1{\n\1    return NativeMethods.\2\n\1}/;s/ref\s+\S+/ref/g;H;g}' file