檔案 1 內容(TOP.txt):
/
/boot
/home
/ptd
/ptd/tcd
/ptd/splunkforwarderdd
檔案 2 內容(POP.txt):
/
/boot
/home
/ptd
/ptd/tcd
/ptd/apps/ddas
輸出檔案 1(Op1.txt):
/ptd/splunkforwarderdd
輸出檔案 2(Op2.txt):
/ptd/apps/ddas
外殼腳本:
while read linesT; do
TOp=$linesT
while read linesP; do
POp=$linesp
if [[ "$TOp" == "$POp" ]]; then
a=cool
else
echo $TOp
fi
done < POp.txt
done < TOp.txt
我確實嘗試過上面的代碼。但它沒有按預期作業。
我希望 TOp.txt 檔案將每一行與 POp.txt 檔案中的每一行進行比較,并將檔案 TOp.txt 中缺少的行顯示為輸出。
與 POp.txt 檔案相同。
uj5u.com熱心網友回復:
您應該使用不同的 fd 單獨閱讀。但是 sort/diff 會好很多
while read -u 3 linesT && read -u 4 linesP; do
TOp=$linesT
POp=$linesp
if [[ "$TOp" == "$POp" ]]; then
a=cool
else
echo $TOp
fi
done 3< TOp.txt 4< POp.txt
uj5u.com熱心網友回復:
假設這兩個檔案的#lines 相同,請嘗試以下操作:
#!/bin/bash
while IFS=$'\t' read -r top pop; do
if [[ $top != $pop ]]; then
echo "$top" >> Op1.txt
echo "$pop" >> Op2.txt
fi
done < <(paste <(sort TOp.txt) <(sort POp.txt))
該paste ...命令將兩個已排序的檔案并排合并,然后將變數合并$top并$pop分配給每一行。
[編輯]
如果兩個檔案的行不平衡,最好使用awk解決方案:
awk 'NR==FNR {t[$0] ; next} # memorize lines in "TOp.txt"
{p[$0] } # memorize lines in "POp.txt"
END {
for (i in t) if (p[i] == "") print i > "Op1.txt" # lines only in "TOp.txt"
for (i in p) if (t[i] == "") print i > "Op2.txt" # lines only in "POp.txt"
}
' TOp.txt POp.txt
uj5u.com熱心網友回復:
comm 比較檔案的行
有趣的選項:
-1 suppress column 1 (lines unique to FILE1)
-2 suppress column 2 (lines unique to FILE2)
-3 suppress column 3 (lines that appear in both files)
--nocheck-order
do not check that the input is correctly sorted
回答:
comm --nocheck-order -23 TOp.txt POp.txt > Op1.txt
comm --nocheck-order -13 TOp.txt POp.txt > Op2.txt
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/410236.html
標籤:
下一篇:傳遞變數和替換變數的子流程
