1

I have a multi-column file that looks like this:

   6 93129B 177        306.98  350.20  8.84    0.23   -0.79    0.22   0.46        m
   7      8 192        269.08  413.35  9.29    0.15   -0.81    0.14   0.33        m
  32             Y582  506.12  797.42 11.60    1.07    0.82                  89  nm
   9     35 316  FA35 -241.54  431.17  9.85    1.30    0.92    0.54           0  nm
  10         61  Y579  765.80  819.03  9.97    0.32   -0.66                  77   m
  11      9 165        346.38  360.62  9.99    0.17   -0.83    0.16   0.41        m
  12     30  71  Y500  720.08   40.65 10.10    0.43   -0.41    0.41   0.85   83   m
  13    127 200 FA127  250.63  -35.95 10.68    0.31   -0.57    0.32   0.72   96   m
  14      3 149        375.53  334.00 10.73    0.28   -0.65    0.23   0.49        m
  15     21 127  Y547  459.69  280.64 10.78    0.35   -0.67    0.28   0.61   81   m
  16    126 195 FA126  268.94   12.08 10.93    0.35   -0.43    0.34   0.77   96   m
  17                   328.30  358.74 10.94    0.17   -0.72    0.16   0.39        m
  18         92        585.72  -41.89 11.02    0.41   -0.44    0.46   1.01        m
  19      4 163  Y551  348.90  302.36 11.04    0.26   -0.68    0.23   0.51   95   m

Note that the columns are ordered but there are many missing elements in many of them. I need to fill these spaces with a string, say -- so it will look like:

   6 93129B 177  --    306.98  350.20  8.84    0.23   -0.79    0.22   0.46   --   m
   7      8 192  --    269.08  413.35  9.29    0.15   -0.81    0.14   0.33   --   m
  32 --     --   Y582  506.12  797.42 11.60    1.07    0.82    --     --     89  nm
   9     35 316  FA35 -241.54  431.17  9.85    1.30    0.92    0.54   --      0  nm
  10     --  61  Y579  765.80  819.03  9.97    0.32   -0.66    --     --     77   m
  11      9 165  --    346.38  360.62  9.99    0.17   -0.83    0.16   0.41   --   m
  12     30  71  Y500  720.08   40.65 10.10    0.43   -0.41    0.41   0.85   83   m
etc...

I've tried the answers given here, here and here but none of those seem to apply to this issue (or at least I can't figure out how to properly apply them).

  • Are the columns tab-delimited or space-delimited? – Anthony Ananich Oct 25 '14 at 16:21
  • Space separated. – Gabriel Oct 25 '14 at 17:46
3

If you have GNU awk (or an equivalent that supports the FIELDWIDTHS variable), and you don't mind the -- marker being right-justified within the field width, then you could do something like

gawk -vFIELDWIDTHS="4 7 4 6 8 8 6 8 8 8 7 5 4" -vOFS= '
  {for (i=1;i<=NF;i++) sub("  $","--",$i);}1
' file

viz.

$ gawk -vFIELDWIDTHS="4 7 4 6 8 8 6 8 8 8 7 5 4" -vOFS= '
  {for (i=1;i<=NF;i++) sub("  $","--",$i);}1
' file
   6 93129B 177    --  306.98  350.20  8.84    0.23   -0.79    0.22   0.46   --   m
   7      8 192    --  269.08  413.35  9.29    0.15   -0.81    0.14   0.33   --   m
  32     --  --  Y582  506.12  797.42 11.60    1.07    0.82      --     --   89  nm
   9     35 316  FA35 -241.54  431.17  9.85    1.30    0.92    0.54     --    0  nm
  10     --  61  Y579  765.80  819.03  9.97    0.32   -0.66      --     --   77   m
  11      9 165    --  346.38  360.62  9.99    0.17   -0.83    0.16   0.41   --   m
  12     30  71  Y500  720.08   40.65 10.10    0.43   -0.41    0.41   0.85   83   m
  13    127 200 FA127  250.63  -35.95 10.68    0.31   -0.57    0.32   0.72   96   m
  14      3 149    --  375.53  334.00 10.73    0.28   -0.65    0.23   0.49   --   m
  15     21 127  Y547  459.69  280.64 10.78    0.35   -0.67    0.28   0.61   81   m
  16    126 195 FA126  268.94   12.08 10.93    0.35   -0.43    0.34   0.77   96   m
  17     --  --    --  328.30  358.74 10.94    0.17   -0.72    0.16   0.39   --   m
  18     --  92    --  585.72  -41.89 11.02    0.41   -0.44    0.46   1.01   --   m
  19      4 163  Y551  348.90  302.36 11.04    0.26   -0.68    0.23   0.51   95   m
  • Excellent answer steeldriver! Thank you very much. – Gabriel Oct 25 '14 at 17:46

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.