Awk 提取注释中带有//aa以及//**/

Awk 提取注释中带有//aa以及//**/,awk,sed,grep,Awk,Sed,Grep,我有一个文件,我需要提取注释中带有/aa以及/*…*/的行,如下所示: //This is me /*Multiple line arguments best way to use*/ Store the variables. Swap it //Done grep "//" file_name | sed 's/\/\///g' 我尝试过这样的单行注释,如下所示: //This is me /*Multiple line arguments best way to

我有一个文件,我需要提取注释中带有
/
aa以及
/*…*/
的行,如下所示:

//This is me
/*Multiple line arguments
best way to
use*/
Store the variables.
Swap it 

//Done
grep "//" file_name | sed 's/\/\///g'
我尝试过这样的单行注释,如下所示:

//This is me
/*Multiple line arguments
best way to
use*/
Store the variables.
Swap it 

//Done
grep "//" file_name | sed 's/\/\///g'
在那里,我得到了单行注释的预期输出

This is me
Done
对于多行注释,如何提取它们之间的行以获得预期的输出

预期产出:

This is me
Multiple line arguments
best way to
use
Done

使用
awk
您可以尝试以下内容吗。按照GNU
awk
中显示的样本编写和测试

awk '{gsub(/^\/\/|^\/\*|\*\/$/,"")} 1' Input_file
简单解释:使用
awk
gsub
(全局替换)功能,在所有行中替换起始
/
或起始
/*
或以
*/
结尾的行,然后简单地打印行


编辑:如果您只想获得注释行,则以下内容可能会有所帮助,仅使用显示的样本编写和测试

awk '
!NF{
  next
}
/^\/\//{
  sub(/^\/\//,"")
  print
  next
}
/^\/\*/{
  sub(/^\/\*/,"")
  multiFound=1
}
/\*\/$/{
  sub(/\*\/$/,"")
  multiFound=""
  print
  next
} 
multiFound
'  Input_file

使用
awk
您可以尝试以下内容吗。按照GNU
awk
中显示的样本编写和测试

awk '{gsub(/^\/\/|^\/\*|\*\/$/,"")} 1' Input_file
简单解释:使用
awk
gsub
(全局替换)功能,在所有行中替换起始
/
或起始
/*
或以
*/
结尾的行,然后简单地打印行


编辑:如果您只想获得注释行,则以下内容可能会有所帮助,仅使用显示的样本编写和测试

awk '
!NF{
  next
}
/^\/\//{
  sub(/^\/\//,"")
  print
  next
}
/^\/\*/{
  sub(/^\/\*/,"")
  multiFound=1
}
/\*\/$/{
  sub(/\*\/$/,"")
  multiFound=""
  print
  next
} 
multiFound
'  Input_file

要仅打印最初要求的注释,请执行以下操作:

awk '
     BEGIN { 
             prnt=0 # Initialise variable to 0
           } 
     /^\/\// { # Process lines starting with //
               $0=gensub("/","","g",$0); # Strip out every /
               print; # print the line
               next # process the next line
             } 
     /^\/\*/ { # process lines with start with /*
               prnt=1 # set the prnt variable to 1
             } 
     /^.*\*\/$/ { # Process lines that end with */
               prnt=0; # Set the prnt variable to 0
               $0=gensub("[/*]","","g",$0); # Strip / and * out of the line
               print 
             } 
     prnt==1 { 
               $0=gensub("[/*]","","g",$0); # When prnt is equal to one, strip the / and * and print
               print 
             } 
     prnt==0 {  # When prnt is 0, ignore the line and skip to the next line.
               next 
             }' file
一艘班轮:

awk 'BEGIN { prnt=0 } /^\/\// { $0=gensub("/","","g",$0);print;next } /^\/\*/ { prnt=1 } /^.*\*\/$/ { prnt=0;$0=gensub("[/*]","","g",$0);print } prnt==1 { $0=gensub("[/*]","","g",$0);;print } prnt==0 { next }' file
输出:

This is me
Multiple line arguments
best way to
use
Done
Awk版本:

GNU Awk 4.0.2
使用gsub而不是GESUB的GNU独立版本:

awk 'BEGIN { prnt=0 } /^\/\// { gsub("/","",$0);print;next } /^\/\*/ { prnt=1 } /^.*\*\/$/ { prnt=0;gsub("[/*]","",$0);print } prnt==1 { gsub("[/*]","",$0);print } prnt==0 { next }' file 

要仅打印最初要求的注释,请执行以下操作:

awk '
     BEGIN { 
             prnt=0 # Initialise variable to 0
           } 
     /^\/\// { # Process lines starting with //
               $0=gensub("/","","g",$0); # Strip out every /
               print; # print the line
               next # process the next line
             } 
     /^\/\*/ { # process lines with start with /*
               prnt=1 # set the prnt variable to 1
             } 
     /^.*\*\/$/ { # Process lines that end with */
               prnt=0; # Set the prnt variable to 0
               $0=gensub("[/*]","","g",$0); # Strip / and * out of the line
               print 
             } 
     prnt==1 { 
               $0=gensub("[/*]","","g",$0); # When prnt is equal to one, strip the / and * and print
               print 
             } 
     prnt==0 {  # When prnt is 0, ignore the line and skip to the next line.
               next 
             }' file
一艘班轮:

awk 'BEGIN { prnt=0 } /^\/\// { $0=gensub("/","","g",$0);print;next } /^\/\*/ { prnt=1 } /^.*\*\/$/ { prnt=0;$0=gensub("[/*]","","g",$0);print } prnt==1 { $0=gensub("[/*]","","g",$0);;print } prnt==0 { next }' file
输出:

This is me
Multiple line arguments
best way to
use
Done
Awk版本:

GNU Awk 4.0.2
使用gsub而不是GESUB的GNU独立版本:

awk 'BEGIN { prnt=0 } /^\/\// { gsub("/","",$0);print;next } /^\/\*/ { prnt=1 } /^.*\*\/$/ { prnt=0;gsub("[/*]","",$0);print } prnt==1 { gsub("[/*]","",$0);print } prnt==0 { next }' file 


谢谢Ravinder的快速解释,上面的解释也起作用了。@Rama,欢迎Rama干杯,学习愉快。@Rama,如果您只想打印注释行,那么编辑解决方案也应该对您起作用,干杯。谢谢Ravinder的快速解释,上面的解释也起作用了。@Rama,欢迎您,Rama干杯,学习愉快。@Rama,如果您只想打印注释行,那么编辑解决方案也应该适合您,干杯。我尝试了上述代码,但没有得到预期的输出。它仅从//和/*检索行。它没有检索/*到*/之间的行。这只是您想要的注释还是像其他解决方案一样的所有输出?我只需要获取注释为显示预期输出的行。可能您的awk版本不同?我已经添加了用于测试解决方案的版本详细信息。谢谢。我已经将其添加到解决方案中。我尝试了上面的代码,但没有得到预期的输出。它仅从//和/*检索行。它没有检索/*到*/之间的行。这只是您想要的注释还是像其他解决方案一样的所有输出?我只需要获取注释为显示预期输出的行。可能您的awk版本不同?我已经添加了用于测试解决方案的版本详细信息。谢谢。我已经将其添加到解决方案中,应该为输出提供什么样的输入行,如
stuff/*foo//bar*/
?此外,您接受的答案不会产生您所期望的输出,因此根本不清楚您真正想要做的是什么。应该给输出一个输入行,如
stuff/*foo//bar*/
?而且,你接受的答案并没有产生你所期望的结果,所以根本不清楚你到底想做什么。