交换文本文件列并创建新文件

时间:2017-02-13 17:10:14

标签: regex bash text-files multiple-columns swap

我在两个文件夹fileAnfileBn中有数百个大文本文件AB,我想从这两个文件夹中交换前六列每个文件。然后,我会将新文件fileCnfileDn保存在文件夹CD中。 AB中的文件数量相同,文件长度相同。

如何使用bash或regex实现这一目标?

以下是fileA1

的摘录
"2015-11-11 00:00:00.00",59841,0.327,3.275,1.89275,32.048,8,32,11.61
"2015-11-11 00:00:00.10",59842,0.086,3.56975,2.20325,32.10205,9,32,11.57
"2015-11-11 00:00:00.20",59843,0.26575,3.343,1.8285,32.06717,10,32,11.57
"2015-11-11 00:00:00.30",59844,-0.1915,3.28175,1.793,32.12122,11,32,11.57

fileB1

"2015-11-11 00:00:00.00",59841,2.48975,2.8535,-0.6575,32.13168,9
"2015-11-11 00:00:00.10",59842,2.38975,3.10525,-0.62025,32.21362,10
"2015-11-11 00:00:00.20",59843,2.224,3.09825,-0.43375,32.19968,11
"2015-11-11 00:00:00.30",59844,2.102,3.029,-0.51825,32.21539,12

新文件fileC1,前六列fileB1替换为fileA1的前六列:

"2015-11-11 00:00:00.00",59841,0.327,3.275,1.89275,32.048,9
"2015-11-11 00:00:00.10",59842,0.086,3.56975,2.20325,32.10205,10
"2015-11-11 00:00:00.20",59843,0.26575,3.343,1.8285,32.06717,11
"2015-11-11 00:00:00.30",59844,-0.1915,3.28175,1.793,32.12122,12

新文件fileD1,前六列fileA1替换为fileB1的前六列:

"2015-11-11 00:00:00.00",59841,2.48975,2.8535,-0.6575,32.13168,8,32,11.61
"2015-11-11 00:00:00.10",59842,2.38975,3.10525,-0.62025,32.21362,9,32,11.57
"2015-11-11 00:00:00.20",59843,2.224,3.09825,-0.43375,32.19968,10,32,11.57
"2015-11-11 00:00:00.30",59844,2.102,3.029,-0.51825,32.21539,11,32,11.57

我查看了paste命令,但找不到将其作为批处理的方法。

1 个答案:

答案 0 :(得分:0)

您可以使用pastecut来交换两个文件中的列,

paste -d, <(cut fileA1 -d, -f1-6) <(cut fileB1 -d, -f7) > fileC1
paste -d, <(cut fileB1 -d, -f1-6) <(cut fileA1 -d, -f7-9) > fileD1