1

I have a texfile with a many lines in it, Example lines:

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

I would like to extract lines featuring a person whose last name starts with a P.

output:

AABBA, 10050, Tom marc peter , POR
IIUSA, 10054, Raff paleen , EUS
1
1
awk -F, '{ n=split($3,a," "); if(tolower(substr(a[n],0,1))=="p") print $0; }'
0

Relying on the number of commas and working back to a word beginning with p/P, you can do this with "any" regular expression program. For instance

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

gives

AABBA, 10050, Tom marc Peter , POR
IIUSA, 10054, Raff paleen , EUS
2
  • Do you also know a command for extr4ct All lines fe4turing a p3rson whose name cont4ins three words.? Dec 6 '15 at 15:59
  • My example only used the commas, and assumes the last name is the last word before the comma. If you had a compound last name (such as "Van der Pool"), that would need different rules. Dec 6 '15 at 16:01
0
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.