2017-11-11 250 views
2

我想交换列(或()中的变量)。如果一行以“FD1”开始,()中的变量需要向右移动。最右边的变量位于最左边。例如,FD1 DFF_0(CK,G5,G10);以及FD1 DFF_0(CK,G5,G10);例如,FD1 DFF_0(CK,G5,G10);以及FD1 DFF_0(CK1,G5,G10)。 - > FD1 DFF_0(G10,CK,G5);[SED/AWK]交换值

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

myfile.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 /原来/新/ G' file.txt的,但我认为我的问题是一个有条件的转变。

任何帮助表示赞赏。

+1

你刚才提到,从开始的行'FD1'应该改变,但在输出预计我所看到的所有行得到了改变?你能否确认一次? – RavinderSingh13

回答

1

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); 

UPDATE

也许更简单的这样

$ 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 
+0

谢谢。第一个解决方案就是我想要的。 –

1
sed -n -r -e '/^FD1/s/\((.*),([^,]*)\)/(\2,\1)/p' -e '/^FD1/!s/\(([^,]*),(.*)\)/(\2,\1)/p' Myfile.txt 
2

解决方案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: ; 

解决第二:如果你想对所有线路的变化,然后以下可能会帮助你一样。

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本身再追加> temp_file && mv temp_file Input_file到上面的代码和它应该飞呢。