我需要組合 2 個具有不同字數的單詞串列的檔案 bash 腳本,我想將它們組合起來,如下所示。
檔案 1:
word1
word2
word3
檔案 2:
8.8.8.8
4.4.4.4
4.4.2.2
5.5.5.5
期望的輸出:
word1,8.8.8.8
word1,4.4.4.4
word1,4.4.2.2
word1,5.5.5.5
word2,8.8.8.8
word2,4.4.4.4
word2,4.4.2.2
word2,5.5.5.5
word3,8.8.8.8
word3,4.4.4.4
word3,4.4.2.2
word3,5.5.5.5
uj5u.com熱心網友回復:
查找檔案中未包含的足夠高的欄位編號(如 100)并(ab)用于join生成笛卡爾積
join -j 100 file1.txt file2.txt
word1 8.8.8.8
word1 4.4.4.4
word1 4.4.2.2
word1 5.5.5.5
word2 8.8.8.8
word2 4.4.4.4
word2 4.4.2.2
word2 5.5.5.5
word3 8.8.8.8
word3 4.4.4.4
word3 4.4.2.2
word3 5.5.5.5
編輯:為了有一個逗號作為列分隔符,使用選項命名它,并讓輸出不以該分隔符開頭(以前是空格,現在是逗號),使用選項-t明確排序:-o
join -j 100 -t, -o 1.1,2.1 file1.txt file2.txt
word1,8.8.8.8
word1,4.4.4.4
word1,4.4.2.2
word1,5.5.5.5
word2,8.8.8.8
word2,4.4.4.4
word2,4.4.2.2
word2,5.5.5.5
word3,8.8.8.8
word3,4.4.4.4
word3,4.4.2.2
word3,5.5.5.5
uj5u.com熱心網友回復:
awk您可以通過將兩個檔案值讀取到單獨的索引陣列中來簡化并獲得靈活性,然后在END規則中,只需回圈以您想要的格式輸出的存盤值,例如
awk '
FNR==NR { f1[ n] = $0; next } # save file_1 in array f1
{ f2[ m] = $0 } # save file_2 in array f2
END {
for (i=1; i<=n; i ) # loop over all f1 values
for(j=1; j<=m; j ) # loop over all f2 values
printf "%s,%s\n", f1[i], f2[j] # output f1[],f2[]
}
' file_1 file_2
示例使用/輸出
有了您的資料file_1,file_2您將擁有:
$ awk '
> FNR==NR { f1[ n] = $0; next } # save file_1 in array f1
> { f2[ m] = $0 } # save file_2 in array f2
> END {
> for (i=1; i<=n; i ) # loop over all f1 values
> for(j=1; j<=m; j ) # loop over all f2 values
> printf "%s,%s\n", f1[i], f2[j] # output f1[],f2[]
> }
> ' file_1 file_2
word1,8.8.8.8
word1,4.4.4.4
word1,4.4.2.2
word1,5.5.5.5
word2,8.8.8.8
word2,4.4.4.4
word2,4.4.2.2
word2,5.5.5.5
word3,8.8.8.8
word3,4.4.4.4
word3,4.4.2.2
word3,5.5.5.5
使用 Bash
readarray您可以在 bash 腳本中使用(同義詞)將兩個檔案讀入陣列中執行完全相同的操作mapfile,例如
#!/bin/bash
usage() { ## simple function to output error and usage
[ -n "$1" ] && printf "error: %s\n" "$1"
printf "usage: %s file_1 file_2\n" "${0##*/}"
}
## validate filenames provided in first 2 arguments exist and are non-empty
[ -s "$1" ] || { usage "file $1 not found or empty"; exit 1; }
[ -s "$2" ] || { usage "file $2 not found or empty"; exit 1; }
readarray -t f1 < "$1" # read file_1 int array f1
readarray -t f2 < "$2" # read file_2 int array f2
for i in "${f1[@]}"; do ## loop over f1
for j in "${f2[@]}"; do ## loop over f2
printf "%s,%s\n" "$i" "$j" ## output combined result
done
done
(注意: awk可能會提供更好的性能)
示例使用/輸出
將腳本保存為cmbfiles.sh您將擁有的:
$ bash cmbfiles.sh file_1 file_2
word1,8.8.8.8
word1,4.4.4.4
word1,4.4.2.2
word1,5.5.5.5
word2,8.8.8.8
word2,4.4.4.4
word2,4.4.2.2
word2,5.5.5.5
word3,8.8.8.8
word3,4.4.4.4
word3,4.4.2.2
word3,5.5.5.5
uj5u.com熱心網友回復:
請您嘗試以下方法:
awk -v OFS="," -v ORS="\r\n" ' # set comma as field separator, CRLF as record separator
NR==FNR && NF>0 {a[ n]=$0; next} # read file2.txt skipping blang lines
NF>0 {for (i=1; i<=n; i ) print $0, a[i]} # print line of file1.txt appending the lines of file2.txt
' file2.txt file1.txt
- 它跳過輸入檔案中的空白行。
- 它附加了考慮用 Excel 打開的 Windows 行尾。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/433637.html
