在 mac 上的排序文件中的模式前插入列定界符
Insert column delimiters before pattern in a sorted file on a mac
生成一个包含来自不同 XML 文件的值的文件。
该文件有 5 列,用“;”分隔如果所有模式都匹配。
第一列 = 中性索引
第二列 = 特定索引 1
第三列 = 文件确实包含 Index1
第四列 = 特定索引 2
第五列 = 文件确实包含 Index2
与 Index2 不匹配的模式(如最后三行)也应有 5 列,而最后两列应与前两行相同。
排序后的文件如下所示:
AAA;AAA.1D1;file_X;AAA.2D1;file_Y
AAA;AAA.1E1;file_A;AAA.2E1;file_B
AAA;AAA.2F1;file_C
BBB;BBB.2G1;file_D
CCC;CCC.1B1;file_H
YYY;YYY.2M1;file_N
期望的结果是:
AAA;AAA.1D1;file_X;AAA.2D1;file_Y
AAA;AAA.1E1;file_A;AAA.2E1;file_B
AAA;;;AAA.2F1;file_C
BBB;;;BBB.2G1;file_D
CCC;CCC.1B1;file_H;;
YYY;;;YYY.2M1;file_N
如果您有任何idea/hint,非常感谢您的帮助!提前致谢!
更新答案
鉴于更新后的要求,我想你想要这样的东西:
awk -F';' 'NF==3 && ~/\.1/{[=10=]=[=10=] ";;"}
NF==3 && ~/\.2/{[=10=]= ";;;" ";" } 1' file
可以写成一行:
awk -F';' 'NF==3 && ~/\.1/{[=11=]=[=11=] ";;"} NF==3 && ~/\.2/{[=11=]= ";;;" ";" } 1' YourFile
原答案
我会用 awk
:
awk -F';' 'NF==3{[=12=]= ";;;" ";" }1' YourFile
AAA;AAA.1D1;file_X;AAA.2D1;file_Y
AAA;AAA.1E1;file_A;AAA.2E1;file_B
AAA;;;AAA.2F1;file_C
BBB;;;BBB.2G1;file_D
YYY;;;YYY.2M1;file_N
也就是说..."run awk
on YourFile
using ';'
as field separator. If there are only 3 fields on any line, recreate the line using the existing first field, three semi-colons and then the other two fields. The 1
at the end, means print the current line`".
如果你用的不多awk
,NF
指的是字段数,[=20=]
指的是整个当前行,</code>指的是行中的第一个字段,<code>
指的是第二个字段等
生成一个包含来自不同 XML 文件的值的文件。
该文件有 5 列,用“;”分隔如果所有模式都匹配。
第一列 = 中性索引
第二列 = 特定索引 1
第三列 = 文件确实包含 Index1
第四列 = 特定索引 2
第五列 = 文件确实包含 Index2
与 Index2 不匹配的模式(如最后三行)也应有 5 列,而最后两列应与前两行相同。
排序后的文件如下所示:
AAA;AAA.1D1;file_X;AAA.2D1;file_Y
AAA;AAA.1E1;file_A;AAA.2E1;file_B
AAA;AAA.2F1;file_C
BBB;BBB.2G1;file_D
CCC;CCC.1B1;file_H
YYY;YYY.2M1;file_N
期望的结果是:
AAA;AAA.1D1;file_X;AAA.2D1;file_Y
AAA;AAA.1E1;file_A;AAA.2E1;file_B
AAA;;;AAA.2F1;file_C
BBB;;;BBB.2G1;file_D
CCC;CCC.1B1;file_H;;
YYY;;;YYY.2M1;file_N
如果您有任何idea/hint,非常感谢您的帮助!提前致谢!
更新答案
鉴于更新后的要求,我想你想要这样的东西:
awk -F';' 'NF==3 && ~/\.1/{[=10=]=[=10=] ";;"}
NF==3 && ~/\.2/{[=10=]= ";;;" ";" } 1' file
可以写成一行:
awk -F';' 'NF==3 && ~/\.1/{[=11=]=[=11=] ";;"} NF==3 && ~/\.2/{[=11=]= ";;;" ";" } 1' YourFile
原答案
我会用 awk
:
awk -F';' 'NF==3{[=12=]= ";;;" ";" }1' YourFile
AAA;AAA.1D1;file_X;AAA.2D1;file_Y
AAA;AAA.1E1;file_A;AAA.2E1;file_B
AAA;;;AAA.2F1;file_C
BBB;;;BBB.2G1;file_D
YYY;;;YYY.2M1;file_N
也就是说..."run awk
on YourFile
using ';'
as field separator. If there are only 3 fields on any line, recreate the line using the existing first field, three semi-colons and then the other two fields. The 1
at the end, means print the current line`".
如果你用的不多awk
,NF
指的是字段数,[=20=]
指的是整个当前行,</code>指的是行中的第一个字段,<code>
指的是第二个字段等