【问题标题】:[SED/AWK]exchange values[SED/AWK]交换值
【发布时间】:2017-11-11 00:46:03
【问题描述】:

我想交换列(或 ( ) 中的变量)。如果一行以“FD1”开头,()中的变量需要向右移动。最右边的变量到最左边。

例如,FD1 DFF_0(CK,G5,G10); --> FD1 DFF_0(G10,CK,G5);

对于其他行,() 中的变量需要向左移动。

我的文件.txt:

  FD1 DFF_0(CK,G5,G10);
  FD1 DFF_1(CK,G6,G11);
  IV  NOT_0(G14,G0);
  IV  NOT_1(G17,G11);
  AN2 AND2_0(G8,G14,G6);
  ND2 NAND2_0(G9,G16,G15);
  NR2 NOR2_0(G10,G14,G11);
  NR2 NOR2_1(G11,G5,G9);

Outfile.txt:

  FD1 DFF_0(G10,CK,G5);
  FD1 DFF_1(G11,CK,G6);
  IV  NOT_0(G0,G14);
  IV  NOT_1(G11,G17);
  AN2 AND2_0(G14,G6,G8);
  ND2 NAND2_0(G16,G15,G9);
  NR2 NOR2_0(G14,G11,G10);
  NR2 NOR2_1(G5,G9,G11);

我知道可以全局替换的 SED 基础知识,sed 's/original/new/g' file.txt,但我认为我的问题是条件转换。

感谢任何帮助。

【问题讨论】:

  • 您提到应该更改从FD1 开始的行,但在预期输出中我看到所有行都已更改?请您确认一次。

标签: awk sed multiple-columns


【解决方案1】:

解决方案 1:如果您只想对以字符串 FD1 开头的行进行更改,那么以下内容可能对您有所帮助。

awk -F'[),(]' '              ##Creating ) comma and ( as field separators in each line of Input_file here by -F option of awk.
/^FD1/{                      ##Checking if a line starts from FD1 then do following.
 print $1"("$4","$2","$3");";##Printing the 1st column then ( then 4th column , 2nd column , 3rd column. I will explain further how columns will be seen here in another snippt of code.
 next                        ##next will skip all further statements.
}
1                            ##Mentioning 1 will print the lines.
' Input_file                 ##Mentioning the Input_file name here.

如何查看字段及其编号如下,以便您了解上面的打印内容。 为了让你明白,我只跑了第一行。

awk -F'[),(]' 'NR==1{for(i=1;i<=NF;i++){print "field number:",i OFS "field value:",$i}}'  Input_file
field number: 1 field value: FD1 DFF_0
field number: 2 field value: CK
field number: 3 field value: G5
field number: 4 field value: G10
field number: 5 field value: ;

解决方案 2:如果您想对所有行进行更改,那么以下内容可能对您有所帮助。

awk -F'[),(]' '               ##Creating ) comma and ( as field separators in each line of Input_file here by -F option of awk.
NF==5{                        ##Checking if number of fields in a line are 5.
  print $1"("$4","$2","$3");";##Printing 1st field ( 4th field comma 2nd field comma 3rd field ) here.
  next                        ##next is awk built-in variable which skips all further statements.
}
NF==4{                        ##Checking if number of fields are 4 in a line.
  print $1"("$3","$2");";     ##printing $1 ( $3 comma $2 ); here.
}'   Input_file               ##Mentioning Input_file name here.

另外,如果您想将输出保存到 Input_file 本身,则将&gt; temp_file &amp;&amp; mv temp_file Input_file 附加到上述代码中,然后它应该会运行。

【讨论】:

    【解决方案2】:

    awk 来救援!

    awk -F'[()]' 'function rotateLeft(x) 
                      {return gensub(/([^,]+),(.*)/,"\\2,\\1",1,x)}
                  function rotateRight(x) 
                      {return gensub(/(.*),([^,]+)/,"\\2,\\1",1,x)}
    
                 {print $1 "(" (/^FD1/?rotateRight($2):rotateLeft($2)) ")" $3}'
    
    FD1 DFF_0(G10,CK,G5);
    FD1 DFF_1(G11,CK,G6);
    IV  NOT_0(G0,G14);
    IV  NOT_1(G11,G17);
    AN2 AND2_0(G14,G6,G8);
    ND2 NAND2_0(G16,G15,G9);
    NR2 NOR2_0(G14,G11,G10);
    NR2 NOR2_1(G5,G9,G11);
    

    更新

    这样可能更简单

    $ awk -F'[()]' 'function rotate(left,x) {
                      one="([^,]+)"
                      rest="(.*)"
                      regex=left?(rest "," one):(one "," rest);
                      return gensub(regex,"\\2,\\1",1,x)}
    
                    {print $1 "(" rotate(/^FD1/,$2) ")" $3}' file
    

    【讨论】:

    • 谢谢。第一个解决方案就是我想要的。
    【解决方案3】:
    sed -n -r -e '/^FD1/s/\((.*),([^,]*)\)/(\2,\1)/p' -e '/^FD1/!s/\(([^,]*),(.*)\)/(\2,\1)/p' Myfile.txt
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-29
      • 2013-05-30
      • 2017-04-23
      • 2011-01-23
      • 2017-09-06
      • 2020-10-29
      • 1970-01-01
      相关资源
      最近更新 更多