Print first word based on 5 last words from a file

Viewed 64

I have a file with below content and I want to print the first word only if it is starting with P and if last 5 characters is 12

File:

papa;1;2;3;4;5;12;12;12;12;12
Peter;12;12;12;12;12
alpha;2;2;2;4;5;6;7;8;9;3;3;3;3;3
Prod;1;2;3;4;1;1;1;1;12;12;12;12;12

Expected output:

Peter  
Prod

My attempt:

cat filename | awk -F '[;]' '{print substr( $0,length($0)-5,length($0))}' 
cat filename | egrep -V 'p|a' | awk -F '[;]' '{print $1}'

But I couldn't join the output.


Update:-

From the comments in the accepted answer, apparently OP says there could be lines less than 5 fields in the final file also.

2 Answers
Related