如何读取值将其附加到第2行,然后将第2行附加到第3行,

时间:2018-01-29 09:58:17

标签: loops awk

我在文件中有大约100个单元格,所以我需要读取文件的名称并将文件名附加到" pin"然后读取第二行并将其附加到第三行"方向"那么这样做的方法是什么

例如

MACRO x1
PIN low {
DIRECTION INPUT ;
PIN high {
DIRECTION INPUT ;


MACRO  u1
PIN S1 {
DIRECTION INPUT ;
PIN S2 {
DIRECTION INOUT ;

我需要什么作为输出

MACRO x1
x1 PIN low
x1 PIN low DIRECTION INPUT ;
x1 PIN low
x1 PIN low DIRECTION INPUT ;

MACRO u1
u1 PIN S1
u1 PIN S1 DIRECTION INPUT ;
u1 PIN S2
u1 PIN S2 DIRECTION INOUT ; 

1 个答案:

答案 0 :(得分:0)

关注awk可能对您有帮助。

awk '
/MACRO/{                           ##Looking for string MACRO here and if that is found then do following actions:
  val=$2;                          ##Creating a variable named val whose value is the 2nd field of the current line.
  flag=1;                          ##Creating a variable named flag here and setting its value to 1 here.
  print;                           ##Print is the out of the box keyword for awk and it will print the current line.
  next                             ##next is awk out of the box command which will skip all further statements.
}
flag && !/DIRECTION INPUT/ && NF{  ##Checking condition here if variable flag is NOT NULL and line is NOT having string DIRECTION INPUT in it and line is NOT blank:
  sub(/{/,"");                     ##Substituting character { with NULL on current line.
  val2=$0;                         ##Creating variable named val2 with value of current line.
  print val,$0                     ##Printing the value of variable val along with current line.
}
/DIRECTION INPUT/{                 ##Checking if current line is having string DIRECTION INPUT in it, if yes then do following:
  print val,val2,$0                ##Printing variable named val, val2 and current line here.
}
' Input_file                       ##Mentioning Input_file name here.

输出如下。

MACRO XYZ
XYZ PIN AB
XYZ PIN AB  DIRECTION INPUT ;
XYZ PIN BC
XYZ PIN BC  DIRECTION INPUT ;
MACRO  GEN
GEN PIN DECSEL_STG2[0]
GEN PIN DECSEL_STG2[0]  DIRECTION INPUT ;
GEN PIN DECSEL_STG1[0]
GEN PIN DECSEL_STG1[0]  DIRECTION INPUT ;