Please help me reformat this file

I am working with a file of the form;

4256 7726 1
6525 716 1
7626 0838 1
8726 7623 2
8625 1563 2
1662 2628 3
1551 3552 3
1542 7984 3
1615 5145 3

I want to convert it so that everytime the separator in the 3rd column changes, it inserts a row with follwing data;

1.00000E+030 1.00000E+00 1.00000E+030

so finally the file should look like

4256 7726 1
6525 1680 1
7626 0838 1
1.00000E+030 1.00000E+00 1.00000E+030
8726 8782 2
8625 1563 2
1.00000E+030 1.00000E+00 1.00000E+030
1662 2628 3
1551 3552 3
1542 7984 3
1615 5145 3

I will appreciate any help on this issue.

This should work for you :wink:

awk '{$0=(_&&_!=$NF)?x RS$0:$0;_=$NF}1END{print x}' x="1.00000E+030 1.00000E+00 1.00000E+030" file

Use gawk, nawk or /usr/xpg4/bin/awk on Solaris.

---------- Post updated at 06:03 AM ---------- Previous update was at 06:03 AM ----------

To keep the forums high quality for all users, please take the time to format your posts correctly.

  1. Use Code Tags when you post any code or data samples so others can easily read your code.
    You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags and by hand.)
  2. Avoid adding color or different fonts and font size to your posts.
    Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.
  3. Be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Reply With Quote

Another way :

awk ' NR > 1 { if($3!=f){print "1.00000E+030 1.00000E+00 1.00000E+030" ; print ;f=$3 ;next } }{ f=$3; print ;next } END{print "1.00000E+030 1.00000E+00 1.00000E+030" }'  File_name.txt
awk '{if(tmp=="")
          tmp=$3
      if($3!=tmp)
      {
          print "1.00000E+030 1.00000E+00 1.00000E+030"
          tmp=$3
      }
      print $0
}'