grep:使用 while 循环时重复计数无效

grep: invalid repetition count(s) when using while loop

所以我使用的是 MacOS 命令行并且有两个文件

文件A.txt

A
B
F

文件B.txt

>A
abcde
>B
efghi
>C
jklmn
>D
opqrs
>E
tuvwx
>F
yz123

我希望它通过文件 A.txt 进行 while 循环,只打印相应的 header 和文件 B.txt

中的内容
>A
abcde
>B
efghi
>F
yz123

当我单独浏览文件 A 中的每一行时,该行有效。 grep -n "\A\,\>\{x;p;}" B.txt

但是当我这样做的时候: While read i; do grep -n "$i\,\>\{x;p;}" B.txt >> newfile.txt; done < A.txt

我收到这个错误: grep: invalid repetition count(s)

我做错了什么?

对于 grep,您可以使用:

/bin/grep -A1 -Ff fileA fileB 
>A
abcde
>B
efghi
--               <--- produces separators
>F
yz123

或者使用 awk:

awk 'NR==FNR{a[[=11=]];next}{sub(/^>/,"")} [=11=] in a {print ">"[=11=];p=1;next} p{print;p=0}' fileA fileB 
>A
abcde
>B
efghi
>F
yz123