喜用兩個物理文件的所有我做的awk做工精細:awk中匹配數/連接兩個文件其中1個是一個變量
awk 'NR==FNR { _[$1]=$2 } NR!=FNR { if(_[$2] != "") print $0" "_[$2]}' codes.txt text.txt
我所試圖做的事:而不是使用物理文件使用一個變量而不是爲第二個文件..
awk 'NR==FNR { _[$1]=$2 } NR!=FNR { if(_[$2] != "") print $0" "_[$2]}' codes.txt $variable
,因爲我不必結果導出到一個物理文件,然後使用這個命令將其他文件值與第一個文件映射...
我曾嘗試
echo $variable|awk 'NR==FNR { _[$1]=$2 } NR!=FNR { if(_[$2] != "") print $0" "_[$2]}' codes.txt
已經沒有工作:(
如這裏問的是例如:
cat text.txt
564 ERR0001
535 ERR0002
cat codes.txt
ERR0001 This_is_error_1
ERR0002 This_is_error_2
awk 'NR==FNR { _[$1]=$2 } NR!=FNR { if(_[$2] != "") print $0" "_[$2]}' codes.txt text.txt
564 ERR0001 This_is_error_1
535 ERR0002 This_is_error_2
awk 'NR==FNR { _[$1]=$2 } NR!=FNR { if(_[$2] != "") print $0" "_[$2]}' codes.txt text.txt |tr "_" " "
564 ERR0001 This is error 1
535 ERR0002 This is error 2
這裏是失敗的:
gg=$(cat text.txt)
echo $gg
564 ERR0001 535 ERR0002
awk 'NR==FNR { _[$1]=$2 } NR!=FNR { if(_[$2] != "") print $0" "_[$2]}' codes.txt $gg |tr "_" " "
awk: (FILENAME=codes.txt FNR=2) fatal: cannot open file `564' for reading (No such file or directory)
IFS=' ';
echo $gg
564 ERR0001
535 ERR0002
awk 'NR==FNR { _[$1]=$2 } NR!=FNR { if(_[$2] != "") print $0" "_[$2]}' codes.txt $gg |tr "_" " "
awk: (FILENAME=codes.txt FNR=2) fatal: cannot open file `564' for reading (No such file or directory)
至於建議加入:
join -1 2 -2 1 text.txt codes.txt
ERR0001 564 This_is_error_1
ERR0002 535 This_is_error_2
join -1 2 -2 1 $gg codes.txt
join: extra operand `ERR0002'
Try `join --help' for more information.
echo $gg
564 ERR0001
535 ERR0002
回答tripleee
echo $gg|join -1 2 -2 1 - codes.txt
ERR0001 564 This_is_error_1
ERR0002 535 This_is_error_2
echo $gg|awk 'NR==FNR { _[$1]=$2 } NR!=FNR { if(_[$2] != "") print $0" "_[$2]}' codes.txt - |tr "_" " "
564 ERR0001 This is error 1
535 ERR0002 This is error 2
我不知道你在說什麼。發佈一些示例輸入和預期輸出。 –
在你的問題工作中做了第二個代碼塊嗎? – Kent
@ kent nope它沒有工作 – Vahid