delete last character in all occurences of string

Hello all,

I have a file containing the following

p1 q1 p2 q2 p1 p2 p3
pr1 pr2 pr1 pr2
pa1 pa2

I want to remove the last character from all strings that start with 'p' and end with '1'. In general, I do not know what is between the first part of the string and the last part of the string.
the result is then:

p q1 p2 q2 p p2 p3
pr pr2 pr pr2
pa pa2

I have tried something like this:

 sed 's/\(p[^ ]*1\)./\1/g' testfile 

which will find all occurences of p*1, but I do not know how to get rid of the last character occuring in the string.

Regards,
B

sed 's/\(p[^ ]*1\)./\1/g' testfile

Should be:

sed 's/\(p[^ ]*\)1/\1/g' testfile
awk '{for(i=1;i<=NF;i++) {if($i~/^p.?1$/) gsub("1$","",$i)}}1' FILE
perl -pe's/\b(p\S*)1\b/$1/g' infile

Wow, three solutions in my three favorite scripting languages!
Thanks! :slight_smile: