合并多个文件,并根据每一列将输出拆分为多个文件

时间:2019-05-04 18:53:51

标签: awk

我在行和列上有许多格式完全相同的csv文件。在下面的示例中,我仅提供3个文件作为输入,但是我有很多具有相同特征的文件

目的是对每个输入文件执行以下操作:

在第3、11和12行中取值。

第一个文件中的示例

6174
15
3

然后,从第17到25行打印第一列。

对所有输入文件执行相同的过程,并输出包含所有已读取文件的所有信息的文件。

示例(输出1)

6174,6176,6178
15,17,14
3,3,4
1.6,1.6,1.6
1.6,1.6,1.6
1.6,1.6,1.6
1.7,1.7,1.7
1.8,1.8,1.8
1.9,1.9,1.9
2.1,2.1,2.1
2.3,2.3,2.3

为所有文件和第一列完成该过程时。其余的列都做同样的事情

最后,创建的总文件输出将为10个文件,因为每个文件中有10列。

输入文件(例如3个)

FILE1

test,,,,,,,,,
,,,,,,,,,
test Number 6174,,,,,,,,,
,,,,,,,,,
date1,,,,,,,,,
date2,,,,,,,,,
,,,,,,,,,
interval 2,,,,,,,,,
,,,,,,,,,
Trace Number 2,,,,,,,,,
codde Identification 15,,,,,,,,,
Start Time Error 3 us,,,,,,,,,
,,,,,,,,,
Frequency,Average,Maximum,Minimum,Average,Maximum,Minimum,Average,Maximum,Minimum
,Amplitude,Amplitude,Amplitude,Phase,Phase,Phase,Distortion,Distortion,Distortion
(Hertz),(mV),(mV),(mV),(degrees),(degrees),(degrees),(%),(%),(%)
1.6,19.5,48.2,-37.8,1.5,1.5,1.5,,,
1.6,23.8,47.5,-47,0.1,-0.1,-0.1,,,
1.6,19,31,-45.8,0.1,-0.1,-0.1,,,
1.7,23.2,48.4,-50.4,0.1,-0.1,-0.1,28,28.3,27
1.8,26.5,48.7,-47.6,0.1,-0.1,-0.1,26,27,25.4
1.9,28.4,52,-52.3,0.1,-0.1,-0.1,24.9,25.4,24
2.1,30.8,54.8,-58.1,0.1,-0.1,-0.1,21.8,24,20.8
2.3,34.5,62.8,-63.5,1.4,-1.4,-1.4,20.1,20.8,18.7

FILE2

test,,,,,,,,,
,,,,,,,,,
test Number 6176,,,,,,,,,
,,,,,,,,,
date1,,,,,,,,,
date2,,,,,,,,,
,,,,,,,,,
interval 2,,,,,,,,,
,,,,,,,,,
Trace Number 2,,,,,,,,,
codde Identification 17,,,,,,,,,
Start Time Error 3 us,,,,,,,,,
,,,,,,,,,
Frequency,Average,Maximum,Minimum,Average,Maximum,Minimum,Average,Maximum,Minimum
,Amplitude,Amplitude,Amplitude,Phase,Phase,Phase,Distortion,Distortion,Distortion
(Hertz),(mV),(mV),(mV),(degrees),(degrees),(degrees),(%),(%),(%)
1.6,18.6,40.6,-35,0.3,0.3,0.3,,,
1.6,23.8,39.3,-40.7,1.1,-1.1,-1.1,,,
1.6,19.8,32.3,-39.5,1.1,-1.1,-1.1,,,
1.7,23.5,41.5,-43,1.1,-1.1,-1.1,19.6,19.7,19.2
1.8,26.8,42.8,-45.4,1.1,-1.1,-1.1,18.7,19.2,18.5
1.9,28.6,45.9,-49.1,1.1,-1.1,-1.1,18.1,18.5,17.7
2.1,30.8,50.6,-52.7,1.1,-1.1,-1.1,17.1,17.7,16.5
2.3,35.3,57.1,-60,2.1,-2.1,-2.1,15.7,16.5,14.5

FILE3

test,,,,,,,,,
,,,,,,,,,
test Number 6178,,,,,,,,,
,,,,,,,,,
date1,,,,,,,,,
date2,,,,,,,,,
,,,,,,,,,
interval 2,,,,,,,,,
,,,,,,,,,
Trace Number 2,,,,,,,,,
codde Identification 14,,,,,,,,,
Start Time Error 4 us,,,,,,,,,
,,,,,,,,,
Frequency,Average,Maximum,Minimum,Average,Maximum,Minimum,Average,Maximum,Minimum
,Amplitude,Amplitude,Amplitude,Phase,Phase,Phase,Distortion,Distortion,Distortion
(Hertz),(mV),(mV),(mV),(degrees),(degrees),(degrees),(%),(%),(%)
1.6,19,41.2,-33.3,0.4,-0.4,-0.4,,,
1.6,24.2,41.3,-47.2,1.9,-1.9,-1.9,,,
1.6,20,33.5,-45.6,1.9,-1.9,-1.9,,,
1.7,23.6,45.8,-50.5,1.9,-1.9,-1.9,24.2,24.2,23.9
1.8,27.1,45.5,-53.5,1.9,-1.9,-1.9,23.5,23.9,23.3
1.9,28.9,52.8,-55,1.9,-1.9,-1.9,22.7,23.3,22.1
2.1,31.3,55.5,-57.5,1.9,-1.9,-1.9,21.3,22.1,20.2
2.3,35.2,64.6,-63.3,2.7,-2.7,-2.7,18.2,20.2,17

输出文件(例如3个/应该是10个文件)

输出1

6174,6176,6178
15,17,14
3,3,4
1.6,1.6,1.6
1.6,1.6,1.6
1.6,1.6,1.6
1.7,1.7,1.7
1.8,1.8,1.8
1.9,1.9,1.9
2.1,2.1,2.1
2.3,2.3,2.3

输出2

6174,6176,6178
15,17,14
3,3,4
19.5,18.6,19
23.8,23.8,24.2
19,19.8,20
23.2,23.5,23.6
26.5,26.8,27.1
28.4,28.6,28.9
30.8,30.8,31.3
34.5,35.3,35.2

输出3

6174,6176,6178
15,17,14
3,3,4
48.2,40.6,41.2
47.5,39.3,41.3
31,32.3,33.5
48.4,41.5,45.8
48.7,42.8,45.5
52,45.9,52.8
54.8,50.6,55.5
62.8,57.1,64.6

这是我用来获取第一个输出的代码。选择第一列。我应该对其余9列做同样的事情。

ls *.csv > filelist.txt 

for f in `cat filelist.txt`; do

p1=`awk -F, 'NR==3{print $1}' $f | awk '{print $NF}'`
p2=`awk -F, 'NR==11{print $1}' $f | awk '{print $NF}'`
p3=`awk -F, 'NR==12{print $1}' $f | awk '{print $(NF-1)}'`

echo $p1 > a-$f
echo $p2 >> a-$f
echo $p3 >> a-$f

sed -n '17,25p' $f | awk -F, '{print $1}'  >> a-$f

paste *a-* | awk -v OFS=',' '{$1=$1}1' > file1

done

rm -f *a-*

谢谢。

0 个答案:

没有答案