我想交换列(或()中的变量)。如果一行以" FD1"开头,则()中的变量需要向右移动。最右边的变量到最左边。
例如,FD1 DFF_0(CK,G5,G10); - > FD1 DFF_0(G10,CK,G5);
对于其他行,()中的变量需要向左移动。
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 / original / new / g' file.txt,但我认为我的问题是有条件的转变。
感谢任何帮助。
答案 0 :(得分: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
附加到上面的代码,那么它应该飞行。
答案 1 :(得分:1)
sed -n -r -e '/^FD1/s/\((.*),([^,]*)\)/(\2,\1)/p' -e '/^FD1/!s/\(([^,]*),(.*)\)/(\2,\1)/p' Myfile.txt
答案 2 :(得分: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);
<强>更新强>
这种方式可能更简单
$ 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