I have a data file I need to edit it is in the form:


As you can the numbers have no spaces and I would like to edit the file in a very specific manner (I will be using it as an input file for simulation work). I would like the file to look like:

 -1.0000000000000001e-001  0.0000000000000000e+000  0.0000000000000000e+000  4.3052618410549812e+009  0.0000000000000000e+000  0.0000000000000000e+000  2.4853118072193338e-015
  2.4106903033391415e-004  4.3586744793222273e-005  4.5561759893187341e-005 -4.0315591956328645e+007 -9.1758824977759705e+003 -2.5181138417225957e+004  2.4853118072193338e-015

I have developed an algorithm to do such an edit and tried it in Notepad++ but, the programs adds "invisible" characters to the file which makes it invalid for my simulation. Here is the algorithm:

  1. find string "-1." and replace with string " -1." (there is one space in front of the negative sign in the replacement)

  2. Repeat step 1 for numbers 2-9.

  3. find string "1." and replace with string " 1." (there are two spaces in front of the 1 in the replacement)

  4. Repeat step 3 for numbers 2-9.

  5. find string "- 1." and replace with "-1." (there are two spaces between the negative sign and 1 in the find string)

  6. Repeat step 5 for numbers 2-9.

I want to do this in a UNIX shell (I am using macbook terminal) as I believe this will not add invisible characters and corrupt my data. Any help guys?

Thanks in advance!!!!!

  • There should be two spaces between the numbers unless it has a negative sign, then only one space as such: -4.361255125222242e-05 2.309505585477205e-05 – user111843 Apr 24 '15 at 20:23
  • 2
    Your expected output doesn't match your input – Stéphane Chazelas Apr 24 '15 at 20:24
  • 1
    Would sed -E 's/[0-9.-]*e-?[0-9]{2}/ &/g' do? – Stéphane Chazelas Apr 24 '15 at 20:25
  • 3
    sed -E 's/[0-9.+-]*e[-+]?[0-9]{2}/ &/g' data.txt or sed -E 's/[0-9.+-]*e[-+]?[0-9]{2}/ &/g' < data.txt. See also sed -E 's/e[-+]?[0-9]{2}/& /g' data.txt – Stéphane Chazelas Apr 24 '15 at 20:29
  • 1
    Excellent Cyrus. Thank you SO much. That worked perfectly. Hmmm, interesting I do not know why Costas version did not work for me :( – user111843 Apr 24 '15 at 20:43
sed -E '
   s/[0-9.+-]*e[-+]?[0-9]{2}/     &/g
   s/ *(.{22}[0-9])/\1/g' < input.txt > output.txt

That assumes GNU or FreeBSD (or derivative like OS/X) sed, or a sed conformant to the next version of the Unix/POSIX standard (for -E).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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