Insert commas

Original content:
ACACCTCAT    129                                              | ACACCTCAT    0
ACACCTCATX   171                                              | ACACCTCATX   0
ACACRESRT    0                                                  ACACRESRT    0
ACACRESRTX   0                                                  ACACRESRTX   0
ACACTCOPY    0                                                  ACACTCOPY    0
ACACTGRP     12                                               | ACACTGRP     0
ACACTIVITY   82                                               | ACACTGRPAU   0
ACACTMXVAL   145                                              | ACACTIVITY   0
                                                              > ACACTMXVAL   0
ACACTMXVDT   0                                                  ACACTMXVDT   0
ACACTREL     0                                                  ACACTREL     0
ACACTSEG     0                                                  ACACTSEG     0
ACADDASSGN   0                                                  ACADDASSGN   0
ACADDLCNTR   4                                                | ACADDLCNTR   0
UKCOTABLE    648                                              <
UKNICAT      37                                               <


Result:
ACACCTCAT,    129,                                              | ACACCTCAT,    0
ACACCTCAT,   171,                                              | ACACCTCATX,   0
ACACRESRT,    0,                                                  ACACRESRT,    0
ACACRESRTX,   0,                                                  ACACRESRTX,   0
ACACTCOPY,    0,                                                  ACACTCOPY,    0
ACACTGRP,     12,                                               | ACACTGRP,     0
ACACTIVITY,   82,                                               | ACACTGRPAU,   0
ACACTMXVAL,   145,                                              | ACACTIVITY,   0
                    ,      ,                                    > ACACTMXVAL,   0
ACACTMXVDT,   0,                                                  ACACTMXVDT,   0
ACACTREL,     0,                                                  ACACTREL,     0
ACACTSEG,     0,                                                  ACACTSEG,     0
ACADDASSGN,   0,                                                  ACADDASSGN,   0
ACADDLCNTR,   4,                                                | ACADDLCNTR,   0
UKCOTABLE,    648,                                              <,
UKNICAT,      37,                                               <,

I know a little bit of sed and awk. If you guru can show me either one of the commands, greatly appreciated. I appreciate any help you can provide.
I tried

sed 's/............./&,/g'

and it insert comma (,) at every 14th position.

Hi,
with gnu sed:

$ cat /tmp/f2.txt 
ACACCTCAT    129                                              | ACACCTCAT    0
ACACCTCATX   171                                              | ACACCTCATX   0
ACACRESRT    0                                                  ACACRESRT    0
ACACRESRTX   0                                                  ACACRESRTX   0
ACACTCOPY    0                                                  ACACTCOPY    0
ACACTGRP     12                                               | ACACTGRP     0
ACACTIVITY   82                                               | ACACTGRPAU   0
ACACTMXVAL   145                                              | ACACTIVITY   0
                                                              > ACACTMXVAL   0
ACACTMXVDT   0                                                  ACACTMXVDT   0
ACACTREL     0                                                  ACACTREL     0
ACACTSEG     0                                                  ACACTSEG     0
ACADDASSGN   0                                                  ACADDASSGN   0
ACADDLCNTR   4                                                | ACADDLCNTR   0
UKCOTABLE    648                                              <
UKNICAT      37                                               <
$ sed -e ':bc;/.\{79\}/!s/$/ /;tbc;s/./&,/13;s/./&,/63;s/./&,/79' /tmp/f2.txt 
ACACCTCAT    ,129                                              ,| ACACCTCAT    ,0 
ACACCTCATX   ,171                                              ,| ACACCTCATX   ,0 
ACACRESRT    ,0                                                ,  ACACRESRT    ,0 
ACACRESRTX   ,0                                                ,  ACACRESRTX   ,0 
ACACTCOPY    ,0                                                ,  ACACTCOPY    ,0 
ACACTGRP     ,12                                               ,| ACACTGRP     ,0 
ACACTIVITY   ,82                                               ,| ACACTGRPAU   ,0 
ACACTMXVAL   ,145                                              ,| ACACTIVITY   ,0 
             ,                                                 ,> ACACTMXVAL   ,0 
ACACTMXVDT   ,0                                                ,  ACACTMXVDT   ,0 
ACACTREL     ,0                                                ,  ACACTREL     ,0 
ACACTSEG     ,0                                                ,  ACACTSEG     ,0 
ACADDASSGN   ,0                                                ,  ACADDASSGN   ,0 
ACADDLCNTR   ,4                                                ,| ACADDLCNTR   ,0 
UKCOTABLE    ,648                                              ,<              ,  
UKNICAT      ,37                                               ,<              , 

Regards.

1 Like

With problems like this, it is always a good idea to tell us what operating system and shell you're using. And, we need a clearer description of what constitutes a field in your input file and where the commas are to be inserted into your output. (I have no idea why the 1st two commas in the line marked in red above are positioned where they are???)

While it is true that the sed command you showed us adds a comma after every string of 13 characters found on each line of its input, I don't see how that is related to the output you displayed in the result you are trying to achieve.

It sort of looks like you have five fixed-width fields and any two of those fields might be missing from an input line. If all of the fields contain non-blank characters, you want to insert a comma at the end of the non-blank characters in fields 1, 2, and 4 if field 4 is present and after fields 1, 2, and 3 if field 4 is missing. And, if field 5 is present, a space is to be added to the end of field 5. But, as I mentioned before, if fields 1 and 2 do not contain any non-blank characters, I don't understand why the commas on that line are where they are in your desired result???

If you aren't on a system with GNU sed , or if the script disedorgue provided doesn't do what you need, please give us a much clearer specification of what you are trying to do and tell us what operating system and shell you're using.

1 Like

Thank you both so much for helping out. I am on windows with cygwin. It seems to work fine for me. Again, thank you!

This might come close to what you desired (except for the nearly empty line and the last and second last one):

sed 's/\([^ ]\)\( \)/\1,\2/g' file
ACACCTCAT,    129,                                              |, ACACCTCAT,    0
ACACCTCATX,   171,                                              |, ACACCTCATX,   0
ACACRESRT,    0,                                                  ACACRESRT,    0
ACACRESRTX,   0,                                                  ACACRESRTX,   0
ACACTCOPY,    0,                                                  ACACTCOPY,    0
ACACTGRP,     12,                                               |, ACACTGRP,     0
ACACTIVITY,   82,                                               |, ACACTGRPAU,   0
ACACTMXVAL,   145,                                              |, ACACTIVITY,   0
                                                              >, ACACTMXVAL,   0
ACACTMXVDT,   0,                                                  ACACTMXVDT,   0
ACACTREL,     0,                                                  ACACTREL,     0
ACACTSEG,     0,                                                  ACACTSEG,     0
ACADDASSGN,   0,                                                  ACADDASSGN,   0
ACADDLCNTR,   4,                                                |, ACADDLCNTR,   0
UKCOTABLE,    648,                                              <
UKNICAT,      37,                                               <