首页 > 解决方案 > awk 解析文本文件问题

问题描述

我刚刚开始学习如何使用 awk 来解析和打印文本文件。

我从下面的代码开始,谁能帮助我?

注意:在输出文件中引用是强制性的(请参阅所需的输出)

 awk '/^IPDATA=/ && /A|B|C| '{print "ADD IP ="$0"\n(\n  \Ref "$1",Type vlan="$2"\"\n)\n"}' file > file1 

注意:在示例中,Ref 是 IPREF 行的总和,我们有三个:[2] && [2] && [1]。

示例文本文件实际上很大,但我总结了它:

IPDATA=A                IPID A            
IPDATA=A                IPREF   [2] =     
--- IPREF = VRID=A_1                      
--- IPREF = VRID=A_2                      
                                          
IPDATA=B                IPID B            
IPDATA=B                IPREF   [2] =     
--- IPREF = VRID=B_1                      
--- IPREF = VRID=B_2                      
                                          
IPDATA=C               IPID C             
IPDATA=C               IPREF    [1] =     
--- IPREF = VRID=C_1                      

我想得到以下结果:

"ADD IP=A "
( Ref  2
"Type vlan=VRID=A_1" 
"Type vlan=VRID=A_2" 
)
"ADD IP=B "          
( Ref  2              
"Type vlan=VRID=B_1" 
"Type vlan=VRID=B_2" 
)
"ADD IP=C "          
( Ref  1              
"Type vlan=VRID=C_1" 
)  

                 

谢谢

标签: unixawk

解决方案


您能否尝试仅在 GNU 中使用所示示例进行跟踪、编写和测试awk

awk -v s1="\"" '
/^IPDATA/ && /IPID .*/{
  if(FNR>1){  print ")"  }
  print s1 "ADD IP" s1 "="s1 $NF OFS s1
  next
}
/^IPDATA.*IPREF.*\[[0-9]+\]/{
  match($0,/\[[^]]*/)
  print "( Ref sum of IPREF " substr($0,RSTART+1,RLENGTH-1)
  next
}
/^--- IPREF/{
  print s1 "Type vlan="$NF s1
}
END{
  print ")"
}
' Input_file

说明:为上述添加详细说明。

awk -v s1="\"" '                                               ##Starting awk program from here.
/^IPDATA/ && /IPID .*/{                                        ##Checking condition if line starts IPDATA and having IPID here.
  if(FNR>1){  print ")"  }                                     ##Checking condition if FNR>1 then printing ) here.
  print s1 "ADD IP" s1 "="s1 $NF OFS s1                        ##Printing s1 ADD IP s1 equal to s1 last field OFS and s1 here.
  next                                                         ##next will skip all further statements from here.
}
/^IPDATA.*IPREF.*\[[0-9]+\]/{                                  ##Checking condition if line starts from IPDATA and having IPREF till [ digits ].
  match($0,/\[[^]]*/)                                          ##Using match to match from [ till ] in line.
  print "( Ref sum of IPREF " substr($0,RSTART+1,RLENGTH-1)    ##printing string as per request and sub-string from RSTART+1 to till RLENGTH-1 here.
  next
}
/^--- IPREF/{                                                  ##Checking conditon if line starts from --- IPREF then do following.
  print s1 "Type vlan="$NF s1                                  ##Printing s1 string last field and s1 here.
}
END{                                                           ##Starting END block of this program from here.
  print ")"                                                    ##Printing ) here.
}
' Input_file                                                   ##Mentioning Input_file name here.

推荐阅读