-2

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
supervirus
  • 97
  • 1
  • 3
  • 10
  • 2
    Please format your examples (try backticks) and clarify the nature of your input. (More examples might help illustrate what is typical.) Why is the `0` removed from `014`? Will there always be exactly only zero to be removed? Is this `RS.PK.03.01.4200.03.014.01` on a line by itself, or hidden within other text? – e0k Feb 19 '16 at 05:12
  • 2
    Please show two or three more representative transformations, so we can see what sort of variable inputs you get, and the corresponding expected outputs. The removal of the leading zero is the most worrying part; the rest is straight-forward. How much variation is there in that seventh field? – Jonathan Leffler Feb 19 '16 at 05:17
  • 1
    Think about it this way: How are we to know that this isn't what you need? `echo "man03140101"` – Jon Carter Feb 19 '16 at 05:18

3 Answers3

4

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
$
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

Your requirements are unclear in several respects, but the following does what you've requested and should at least get you close to your destination:

awk -F. '{sub(/^0/,"",$7);          #  "0" was removed from "014"
   print "man" $6 $7 $8 "01" }'

(If you're new to awk, I'd suggest starting by reading up on BEGIN, FS, OFS, and sub.)

peak
  • 105,803
  • 17
  • 152
  • 177
0

This might work for you (GNU sed):

sed -rn 's/.*\.(.*)\.0?(.*)\.(.*)$/man\1\2\301/p'

This pattern matches on the last three .'s and uses grouping and backreferences to build the required string. Notice man and 01 are prepended and appended to the backreferences 1, 2 and 3. Also that 0 is optional before backreference number 2.

potong
  • 55,640
  • 6
  • 51
  • 83