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

相關內容