我有三个文件, a , b 和 c 。 c 有一个代码列表。 b 有两列:一列代码及其对应的test
名称。最后一个文件 a 有一个名称的列表,其中包含(作为子字符串)所有测试名称。例子:
C :
codeb coded codea codec codee codef codee codeg codeh
B'/强>:
codea testa codeb testb codec testc coded testa codee testa codef testb codeg testc codeh testa
一个:
testa1234 testb21345 14231testcAr
我想在 c 中的每个代码的 a 文件中输出相应的名称。例如,codeb
应输出testb21345
。我无法让它发挥作用。我认为这与grep不了解模式有关。这是我写成MVE的循环:
diractual=$PWD
while read line; do
ca=$(grep $line $diractual/b | cut -f 2)
ca_complete=$(grep $ca $diractual/a)
echo "This is ca:"
echo "$ca"
echo "This is ca_complete:"
echo "$ca_complete"
done <$diractual/c
两个echo
s 应输出,例如codeb
( c 中的第一行):
This is ca:
testb
This is ca_complete:
testb21345
但它输出(对于每一行):
This is ca:
testb
This is ca_complete:
#(Empty line)
因此,第一个grep
找到了正确的test
,并将其存储在变量ca
中,但第二个却未在 a 中找到此模式
答案 0 :(得分:1)
与使用bash和grep不同,使用单个awk调用生成所需的输出会更简单,也可能更快。例如,对于thr ARGIND
变量的GNU awk,您可以编写:
$ gawk 'ARGIND==1{a[$1]=$2}ARGIND==2{b[$1]}ARGIND==3{for(i in b) if ($0 ~ a[i]) print i, $0}' b c a
codeh testa1234
codea testa1234
coded testa1234
codee testa1234
codef testb21345
codeb testb21345
codeg 14231testcAr
codec 14231testcAr
以更易阅读的格式:
gawk ' ARGIND == 1 { a[$1] = $2 }
ARGIND == 2 { b[$1] }
ARGIND == 3 {
for (i in b)
if ($0 ~ a[i])
print i, $0
}' b c a
答案 1 :(得分:0)
如果我理解正确
filea="a"
fileb="b"
filec="c"
while read -r code
do
printf "%s: %s\n" "$code" "$(grep "$(grep -oP "^$code\s+\K.*" "$fileb")" "$filea")"
done < "$filec"
打印
codeb: testb21345
coded: testa1234
codea: testa1234
codec: 14231testcAr
codee: testa1234
codef: testb21345
codee: testa1234
codeg: 14231testcAr
codeh: testa1234
或分为单独的步骤
while read -r code
do
tst=$(grep -oP "^$code\s+\K.*" "$fileb")
res=$(grep "$tst" "$filea")
printf "%s\t%s\t%s\n" "$code" "$tst" "$res"
done < "$filec"
打印
codeb testb testb21345
coded testa testa1234
codea testa testa1234
codec testc 14231testcAr
codee testa testa1234
codef testb testb21345
codee testa testa1234
codeg testc 14231testcAr
codeh testa testa1234