Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hello I have a lot of lines like below:


I just want to change the number format (15 digit) XXXXXXXXXXXXXXX to XXXXXXXXX;XXX;XXX



with sed.

any other format just leave the default

share|improve this question
up vote 1 down vote accepted

Assuming that "X" and "Y" represent digits and that your example line actually has 9 "Y" digits (it has eight in your question, but the split you want shows 9), this should work:

sed 's/;\([0-9]\{6\}\)\([0-9]\{3\}\);/;\1;\2;/;s/;\([0-9]\{9\}\)\([0-9]\{3\}\)\([0-9]\{3\}\);/;\1;\2;\3;/'

You could use variables to make it perhaps a little more readable:

sed "s/;\($dig6\)\($dig3\);/;\1;\2;/;s/;\($dig9\)\($dig3\)\($dig3\);/;\1;\2;\3;/"
share|improve this answer

when you have structured data and field delimiters, its easier to use awk. In your data, you have ";" as delimiters, so use it.

awk -F";" '
' OFS=";" file

The awk statement says use ";" as delimiters, then work on field 2 and 4 ($2, $4) for your desired output. To change the format, use substringing. Finally set the output field separator back to ";".

this is clearer than using long cluttered regex with sed.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.