首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >搜索图案并打印计数

搜索图案并打印计数
EN

Stack Overflow用户
提问于 2020-11-03 11:58:18
回答 2查看 106关注 0票数 2

我有如下的输入表

代码语言:javascript
运行
复制
module      input        output    type           func
AOIO        A1,A2,B       Z         comb        ((!A1+!A2) B)
AN2         A1,A2         ZN        comb        (A1 A2)
AIO         A1,A2,A3      Z          comb       ((A1 A2)+A3)
NOR         A1,A2 ,B       Z        comb       (!((A1 A2)+B))
BUF         I             Q          seq         I
CLK         I             QN         seq        IN
BND     
INV         IN            Z           comb      (!I)      

我希望上面的桌子以下面的格式显示。

代码语言:javascript
运行
复制
module      input        output    type           func                 path
AOIO        A1,A2,B       Z         comb        ((!A1+!A2) B)           3
AN2         A1,A2         ZN        comb        (A1 A2)                 1
AIO         A1,A2,A3      Z          comb       ((A1 A2)+A3)            2
NOR         A1,A2 ,B       Z        comb       (!((A1 A2)+B))           3
BUF         I             Q          seq         I                      1
CLK         I             QN         seq        IN                      1
BND                                                                     0
INV         IN            Z           comb      (!I)                    1

获取路径的逻辑是

数开括号'(‘)。

如果(!结尾,那么将路径赋值为count+1,就像AO的情况一样。如果它没有!,那么将路径赋值为计数,即只有no.of开括号(a,AI情况)。如果它没有开始括号,指定路径为1 (BUF,CLK大小写)。如果它没有func,那么将路径赋值为0(BND大小写)。如果!在开括号之间,(,则忽略!并计算开括号的数目,并将其分配给path (在NOR情况下)。每当函数中存在(!I)时,将路径赋值为1始终。

有人能帮我建立这方面的逻辑吗。我可以计数(使用下面的代码并获得no的值)。(然后可以使用以下方法检查上述条件:

代码语言:javascript
运行
复制
sed 's/[^(]//g'  file  | awk '{ print length}  |  awk '{ if ($2~/^(*!/ ) print $3 length+1 ; elseif($2~/^( / ) print $3 length; elseif ($2 !~ /(/ print $3 "1"; elseif ($2~/(!I)/ ) print $3 "1"

但是在这里,在代码中,我没有得到“(”和得到错误)的计数。我不懂逻辑,如果'!‘,我怎么能数开括号呢?介于两者之间。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-11-03 14:57:23

你也可以试试这个awk:

代码语言:javascript
运行
复制
cat cpath.awk
BEGIN {
    FS = " {3,}"
}
NR == 1 {
   printf "%-30s %s\n", $0, "path"
   next
}
{
    n = 0

    if ($2 ~ /!I|^[^(]+$/)
       n = 1
    else if (match($2, /^\(+![^(]/))
       n = RLENGTH -1
    else if (match($2, /^\([!(]*/)) {
       s = substr($2, 1, RLENGTH)
       n = gsub(/!$|\(/, "", s)
    }

    printf "%-30s %s\n", $0, n
}

然后将其降为:

代码语言:javascript
运行
复制
awk -f cpath.awk file
代码语言:javascript
运行
复制
module      input         output   type     func             path
AOIO        A1,A2,B       Z        comb     ((!A1+!A2) B)    3
AN2         A1,A2         ZN       comb     (A1 A2)          1
AIO         A1,A2,A3      Z        comb     ((A1 A2)+A3)     2
NOR         A1,A2 ,B      Z        comb     (!((A1 A2)+B))   3
BUF         I             Q        seq      I                1
CLK         I             QN       seq      IN               1
BND                                                          0
INV         IN            Z        comb     (!I)             1
票数 2
EN

Stack Overflow用户

发布于 2020-11-03 14:10:27

编辑:添加解决方案,因为OP的示例发生了更改。

代码语言:javascript
运行
复制
awk '
FNR==1{
  OFS="\t"
  print $0,"path"
  next
}
{
  line=$0
  if($0~/!I/){
     print $0,1
     next
  }
  sub(/[^(]*/,"")
  sub(/[a-zA-Z0-9].*/,"")
  if($0~/!$/){
     print line,length($0)
  }
  else if(!NF){
     print line,length($0)+1
  }
  else if($0 ~ /!/ && $0 !~ /!$/){
     print line,length($0)-1
  }
  else{
     print line,length($0)
  }
}' Input_file | column  -t -s $'\t'

输出如下。

代码语言:javascript
运行
复制
module      input        output    type           func         path
AOIO        A1,A2,B       Z         comb        ((!A1+!A2) B)  3
AN2         A1,A2         ZN        comb        (A1 A2)        1
AIO         A1,A2,A3      Z          comb       ((A1 A2)+A3)   2
NOR         A1,A2 ,B       Z        comb       (!((A1 A2)+B))  3
BUF         I             Q          seq         I             1
CLK         I             QN         seq        IN             1
BND                                                            1
INV         IN            Z           comb      (!I)           1

请您试一试,基于您所显示的示例仅在GNU awk中。

代码语言:javascript
运行
复制
awk '
FNR==1{
  OFS="\t"
  print $0,"path"
  next
}
{
  line=$0
  if($0~/!I/){
     print $0,1
     next
  }
  $1=""
  sub(/[^\t]*\t+/,"")
  sub(/[a-zA-Z0-9].*/,"")
  if($0~/!$/){
     print line,length($0)
  }
  else if(!NF){
     print line,length($0)+1
  }
  else if($0 ~ /!/ && $0 !~ /!$/){
     print line,length($0)-1
  }
  else{
     print line,length($0)
  }
}' Input_file | column  -t -s $'\t'

输出如下。

代码语言:javascript
运行
复制
module     func            path
AO         ((!A1+!A2) B)   3
AN         (A1 A2)         1
AI         ((A1 A2)+A3)    2
NOR        (!((A1 A2)+B))  3
BUF         I              1
CLK         IQ             1
BND                        0
INV        (!I)            1

解释:添加了上面的详细说明。

代码语言:javascript
运行
复制
awk '                                   ##Starting awk program from here.
FNR==1{                                 ##Checking condition if this is first line then do following.
  OFS="\t"                              ##Setting OFS as tab here.
  print $0,"path"                       ##Printing current line and path string here.
  next                                  ##next will skip all further statements from here.
}
{
  line=$0                               ##Setting variable line to current line.
  if($0~/!I/){                          ##Checking condition if line is equal to !I then do following.
     print $0,1                         ##Printing current line and 1 here.
     next                               ##next will skip all further statements from here.
  }
  $1=""                                 ##nullifying first field here.
  sub(/[^\t]*\t+/,"")                   ##Substituting everything till tab and tab occurrences with NULL here.
  sub(/[a-zA-Z0-9].*/,"")               ##Substituting everything till last in current line.
  if($0~/!$/){                          ##Checking if line do not end with ! then do following.
     print line,length($0)              ##Printing line and length of current line.
  }
  else if(!NF){                         ##else if number of fields is NULL then do following.
     print line,length($0)+1            ##Printing line and length of current line +1 here.
  }
  else if($0 ~ /!/ && $0 !~ /!$/){      ##else if line is NOT equal to ! AND not ending with !
     print line,length($0)-1            ##Printing line and length of current line-1 here.
  }
  else{                                 ##else do following.
     print line,length($0)              ##Printing line and length of current line.
  }
}' Input_file | column  -t -s $'\t'     ##Passing awk output to column command to separate it tab wise.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64662412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档