如何提取以某个字母开头的行?

如何提取以某个字母开头的行?

我有一个 texfile,其中有很多行,示例行:

AABBA, 10050, Tom marc Peter , POR
BBBAC, 10051, Eva braun, AUS
CCAHA, 10052, Jon murry , UDD
GGHAN, 10053, Taler yon, AGG
IIUSA, 10054, Raff paleen , EUS
UUGAS, 10055, Bara enty, ESP

我想提取姓氏以 P 开头的人的行。

输出:

AABBA, 10050, Tom marc peter , POR
IIUSA, 10054, Raff paleen , EUS

答案1

awk -F, '{ n=split($3,a," "); if(tolower(substr(a[n],0,1))=="p") print $0; }'

答案2

根据逗号的数量并返回以 p/P 开头的单词,您可以使用“任何”正则表达式程序来执行此操作。例如

grep -E '^([^,]+,){2,2}[^,]+[[:space:]][pP][^[:space:],]+[[:space:]]*,' mytextfile

给出

AABBA, 10050, Tom marc Peter , POR
IIUSA, 10054, Raff paleen , EUS

答案3

grep -ie' p[^, ]* *,[^,]*$'  \
<<""
AABBA, 10050, Tom marc Peter , POR
BBBAC, 10051, Eva braun, AUS
CCAHA, 10052, Jon murry , UDD
GGHAN, 10053, Taler yon, AGG
IIUSA, 10054, Raff paleen , EUS
UUGAS, 10055, Bara enty, ESP

AABBA, 10050, Tom marc Peter , POR
IIUSA, 10054, Raff paleen , EUS

相关内容