Search code examples
awksedtext-manipulation

Sed/awk text manipulation


How can I convert this :

RS.PK.03.01.4200.03.014.01 to man03140101?

What needs to be done?

  • Text before 5th "." has to be removed : RS.PK.03.01.4200.
  • The remaining text should be converted 03.014.01 should be converted to 031401 — "0" was removed from "014".
  • 01 be added to the end: 03140101
  • Add man in the front: man03140101

I need to do hundreds of such conversions in a file.


Solution

  • One possibility, using awk:

    awk -F. '{ printf "man%.2d%.2d%.2d01\n", $6, $7, $8 }'
    

    Output:

    $ echo RS.PK.03.01.4200.03.014.01 |  awk -F. '{ printf "man%.2d%.2d%.2d01\n", $6, $7, $8 }'
    man03140101
    $