dimanche 7 octobre 2018

using awk and gensub to remove the part in a string ending with "character+number+S"

My goal is to remove the end "1S" as well as the letter immediately before it, in this case "M". How do I achieve that? My non-working code :

echo "14M3856N61M1S" | gawk '{gensub(/([^(1S)]*)[a-zA-Z](1S$)/, "\\1", "g") ; print $0}'
>14M3856N61M1S

The desired results should be

>14M3856N61

Some additional information here . 1. I do not think substr will work here since my actual target strings would come with various lengths. 2. I prefer not to take the approach of defining special delimiter because this would be used together with "if" as part of the awk conditional operation while the delimiter is already defined globally. Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire