我制作了一個專案,我想使用粘貼加入檔案,但我想這樣做
List1 List2
a 1
b 2
c 3
Result
a1
a2
a3
b1
b2
b3
c1
c2
c3
有沒有辦法用粘貼來獲得這個結果?
uj5u.com熱心網友回復:
有bash兩個簡單的回圈:
while read -r l1; do while read -r l2; do echo "$l1$l2"; done <list2; done <list1
輸出:
a1 a2 a3 b1 b2 b3 c1 c2 c3
uj5u.com熱心網友回復:
以相反的順序將檔案提供給awk:
$ awk 'FNR==NR{a[ cnt]=$1;next}{for (i=1;i<=cnt;i ) print $1 a[i]}' f2 f1
a1
a2
a3
b1
b2
b3
c1
c2
c3
擴展這個黑客:
$ 加入 -j 9999999 -o 1.1,2.1 f1 f2 | sed's///'
a1
a2
a3
b1
b2
b3
c1
c2
c3
uj5u.com熱心網友回復:
使用 python,請您嘗試以下操作(如 Marcus Müller 評論):
#!/usr/bin/python
import itertools
with open('list1', 'r') as f1:
i1 = f1.read().splitlines()
with open('list2', 'r') as f2:
i2 = f2.read().splitlines()
for v1, v2 in itertools.product(i1, i2):
print(v1 v2)
uj5u.com熱心網友回復:
使用 bash,讀取每個檔案一次:
mapfile -t list1 < file1
mapfile -t list2 < file2
brace_expr=$(IFS=,; printf '{%s}{%s}' "${list1[*]}" "${list2[*]}")
eval "printf '%s\n' $brace_expr"
但這很容易受到代碼注入的影響:您最好 100% 確定檔案的內容是安全的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/462189.html
標籤:重击
上一篇:根據下拉選擇更新內容
下一篇:如何用帶分隔符的多行添加行首?
