我正在努力实现的目标
我有一个包含数百个文件的文件夹,每个文件都具有相同的结构,这是一个示例:
Start Date End Date Code1 Code2 Vendor Identifier Quantity V1_1 V1_2 Currency V1_3 ID V1_4 V2 V3 V4 TypeID OtherID Country_of_Sale V5 V6 V7 V8
11/27/16 12/31/16 character_value character_value 2 USD numeric_value character_value character_value character_value character_value AU
11/27/16 12/31/16 character_value character_value 1 USD numeric_value character_value character_value character_value character_value AU
11/27/16 12/31/16 character_value character_value 1 USD numeric_value character_value character_value character_value character_value AU
row count 3558
Country_of_Sale TotalA TotalB TotalC TotalD spu TotalE V2_1 V2_2 TotalF V2_3 V2_4
AR 0 2782223 2782223 7763.1 0.002790251 22 0.05 0.05 4626.17 5023
US 0 2497603034 2497603034 2958948.67 0.001184715 111374 109.33 109.33 1763291.86 1897441
DO 0 529132 529132 632.54 0.001195429 5 0.01 0.01 376.94 403
EC 0 794440 794440 1669.63 0.002101644 14 0.02 0.02 994.96 1087
BR 0 24397952 24397952 57932.77 0.002374493 217 0.43 0.43 34523.2 37225
Ctotal 109.84
Stotal 5680.38
Total 5790.22
如您所见,每个文件实际上应该是两个独立的文件;一个带标题行
Start Date End Date Code1 Code2 Vendor Identifier Quantity V1_1 V1_2 Currency V1_3 ID V1_4 V2 V3 V4 TypeID OtherID Country_of_Sale V5 V6 V7 V8
还有一个带标题行的
Country_of_Sale TotalA TotalB TotalC TotalD spu TotalE V2_1 V2_2 TotalF V2_3 V2_4
分隔这两行的行总是 $1 == row count (/^row count/?)
我想要两个结果文件,一个用于上述每个标题行。 但是同样有数百个文件——所有这些文件都在一个目录中——可以从以下位置提取这些文件:
问题
我知道我的解决方案在于 awk。我不知道哇。我已经研究了几个小时,我已经想出了如何解决这个问题的不同部分,但就是想不出如何将它们整合在一起。
我最终需要的是可以在 Country_of_Sale
上连接(在 SQL 中)的两个表。
预期结果
简单:
文件1:
Start Date End Date UPC ISRC/ISBN Vendor Identifier Quantity V1_1 V1_2 Currency V1_3 ID V1_4 V2 V3 V4 TypeID OtherID Country_of_Sale V5 V6 V7 V8
11/27/16 12/31/16 character_value character_value 2 USD numeric_value character_value character_value character_value character_value AU
11/27/16 12/31/16 character_value character_value 1 USD numeric_value character_value character_value character_value character_value AU
11/27/16 12/31/16 character_value character_value 1 USD numeric_value character_value character_value character_value character_value AU
文件2
Country_of_Sale TotalA TotalB TotalC TotalD spu TotalE V2_1 V2_2 TotalF V2_3 V2_4
AR 0 2782223 2782223 7763.1 0.002790251 22 0.05 0.05 4626.17 5023
US 0 2497603034 2497603034 2958948.67 0.001184715 111374 109.33 109.33 1763291.86 1897441
DO 0 529132 529132 632.54 0.001195429 5 0.01 0.01 376.94 403
EC 0 794440 794440 1669.63 0.002101644 14 0.02 0.02 994.96 1087
BR 0 24397952 24397952 57932.77 0.002374493 217 0.43 0.43 34523.2 37225
我试过的(应要求 :))
我从这个开始:
gawk '
/^row count/ {nextfile}
NR == 1 {$0 = "Filename" OFS $0; print}
FNR > 1 {$0 = FILENAME OFS $0; print}
' OFS='\t' dir/to/raw/files/*.txt > dir/to/munged/file/file1.txt
和
gawk 'FNR==1,/^Country_Of_Sale/{next} /^CTotal/ {nextfile}
{ $0 = FILENAME OFS $0; print }' OFS='\t' dir/to/raw/files/*.txt > dir/to/munged/file/file2.tsv
这有点管用,但我想在一行中完成。
所以我弄乱了这个的各种排列:
awk -F, '{print > $1}' file1
但老实说,我不太明白。我对已经争论不休的数据更满意。
我希望我在这里已经付出了足够的努力。我当然不想利用资源。
最佳答案
假设您的文件具有 .txt
扩展名,并且您希望将生成的文件命名为 .txt.1
或 .txt.2
扩展名,你可以尝试类似的东西:
awk 'BEGINFILE{f=FILENAME".1"} /^row count/{f=FILENAME".2";next} /^Ctotal/{nextfile} {print>f}' *.txt
解释:
在开始处理每个输入文件时,变量
f
设置为FILENAME.1
其中FILENAME
(awk内置变量)是当前进程文件的名称。当当前输入文件的当前行以
row count
开始时,变量f
被设置为FILENAME.2
并且该行被跳过。当当前输入文件的当前行以
Ctotal
开头时,将跳过文件的其余部分。变量
f
用作所有非跳过行的输出文件名。
关于bash - 根据行结构拆分文件夹中的所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47707078/