喜用两个物理文件的所有我做的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