awk 将多个文件中的行按顺序合并到一个文件中

awk 将多个文件中的行按顺序合并到一个文件中

我有很多文件,需要按三个文件一组合并其中的字符串。每个文件有约 100-500 行,生成的文件约为 100-200Mb。

ppc关键字生成器做同样的事情,但速度很慢; 100万个关键词后,1个结果文件需要2-3小时。

f1

Acapella Audio Arts
Acoustic Smart

f2

australian
Australia
America

f3

Round Grille
Square Grille
grille
defence grille

结果

Acapella Audio Arts australian Round Grille
Acapella Audio Arts australian Square Grille
Acapella Audio Arts australian grille
Acapella Audio Arts australian defence grille
Acapella Audio Arts Australia Round Grille
Acapella Audio Arts Australia Square Grille
Acapella Audio Arts Australia grille
Acapella Audio Arts Australia defence grille
Acapella Audio Arts America Round Grille
Acapella Audio Arts America Square Grille
Acapella Audio Arts America grille
Acapella Audio Arts America defence grille
Acoustic Smart australian Round Grille
Acoustic Smart australian Square Grille
Acoustic Smart australianе grille
Acoustic Smart australian defence grille
Acoustic Smart Australia Round Grille
Acoustic Smart Australia Square Grille
Acoustic Smart Australia grille
Acoustic Smart Australia defence grille
Acoustic Smart America Round Grille
Acoustic Smart America Square Grille
Acoustic Smart America grille
Acoustic Smart America defence grille 
etc.....

答案1

只需使用 bash:

while IFS= read -u3 -r product; do
    while IFS= read -u4 -r region; do
        while IFS= read -u5 -r option; do
            echo "$product $region $option"
        done 5<f3
    done 4<f2
done 3<f1

此版本的文件 I/O 较少(每个文件仅读取一次),因此速度可能会更快

mapfile -t products < f1
mapfile -t regions  < f2
mapfile -t options  < f3
for product in "${products[@]}"; do 
    for region in "${regions[@]}"; do
        for option in "${options[@]}"; do
            echo "$product $region $option"
        done
    done
done

相关内容