How to combing output of cut commands with a delimiter?

While looping through a file, I am cutting different length of characters (based on their length) like columns and want to produce the output in a separate file with different columns being separated by a comma.
How to achieve this with an online command. I don't want to create multiple variables or files.

Input file :
123456789
CARISGOOD
CATISBAD
GOAISBAD
GODISGOOD

Output should look like this :
123,45,6789
CAR,IS,GOOD
CAT,IS,BAD
GOA,IS,BAD
GOD,IS,GOOD

Here is the real file

 
  111MN EDXBB35B            11SAP22             00ES ABCD00001000010000000000010000GH20140502
  
  

And the output should be in below format :

         Col=4-6
             Col=7-8
             Col=9-26
             Col=27-28
             Col=29-46
             Col=47-48
             Col=49-51
             Col=52-53
             Col=54-55
             Col=56-64
             Col=65-69
             Col=70-76
             Col=77-81
        
sed 's#\(...\)\(..\)#\1,\2,#' file
1 Like

Thanks, can you please go through the new example and let me know how to accomplish this

if you have gawk, you can use its FIELDWIDTHS capability - I might be off for some fields - I'll leave it up to you to adjust as needed:

gawk -v FIELDWIDTHS='6 2 17 2 17 2 2 2 2 2 8 4 6 4' '$1=$1' OFS=, myFile

Processing your sample line, results in:

  111M N  EDXBB35B              11SAP22                00 ES  A BCD00001 0000 100000 0000

NOTE: if you don't have gawk/FIELDWIDTHS, there's a way to implement it in other awk-s.

First, it is not nice to go back and edit your original problem statement after your problem has been solved. Second, your updated problem statement is extremely vague.

What output are you hoping to get from your sample real file? Do you want commas inserted between the fields identified by Col=start_col-end_col ? Do you want thirteen lines of output for every input line?

Don, I will keep this in mind going forward.
Your first assumption is correct, I would like to insert commas between the fields identified by the length from the sample line given.

This still leaves a lot of unanswered questions:

  1. Is your program supposed to read a list of Col=start,stop values from a file, or are these fixed values?
  2. Is there supposed to be a comma added before column 4 (or before the 1st identified field)?
  3. Is there supposed to be a comma added after column 81 (or after the last identified field)?
  4. If the Col=values entries are supposed to be read from a file:
    [list=a]
  5. are they always in increasing order?
  6. do they always specify adjacent fields?
    [/list]

Is your program supposed to read a list of Col=start,stop values from a file, or are these fixed values?
Ans : The col=start, stop values are fixed as mentioned.
Is there supposed to be a comma added before column 4 (or before the 1st identified field)?
Ans: No, the first identified field will start from character 4 of the the given sample line.
Is there supposed to be a comma added after column 81 (or after the last identified field)?
Ans : No, there shouldn't be any comma after the last identified field.
If the Col=values entries are supposed to be read from a file:
are they always in increasing order?
do they always specify adjacent fields?
Ans : The values are always fixed as mentioned.

Here is an awk approach:

awk '
        BEGIN {
                D[++c] = "4-6"
                D[++c] = "7-8"
                D[++c] = "9-26"
                D[++c] = "27-28"
                D[++c] = "29-46"
                D[++c] = "47-48"
                D[++c] = "49-51"
                D[++c] = "52-53"
                D[++c] = "54-55"
                D[++c] = "56-64"
                D[++c] = "65-69"
                D[++c] = "70-76"
                D[++c] = "77-81"
        }
        {
                f = 1
                for ( i = 1; i <= c; i++ )
                {
                        split ( D, T, "-" )
                        if ( f )
                                printf "%s", substr ( $0, T[1], (T[2] - T[1]) + 1 )
                        else
                                printf ",%s", substr ( $0, T[1], (T[2] - T[1]) + 1 )
                        f = 0
                }
                printf ",%s\n", substr ( $0, T[2] )
        }
' file