Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

COBOL SEQQUENTIAL FILE TO CSV 1

Status
Not open for further replies.

AMCM

Programmer
Feb 22, 2023
18
MR
Hello ,
im trying to convert sequential cobol file to csv using data i found online.
Code:
IDENTIFICATION DIVISION.
       PROGRAM-ID.  CPYFILES.
         AUTHOR.  M.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       SELECT MY-INPUT-FILE ASSIGN TO
       "C:\Users\Hp\Documents\my_input.txt"
               ORGANIZATION IS LINE SEQUENTIAL.
       SELECT MY-OUTPUT-FILE ASSIGN TO
       "C:\Users\Hp\Documents\my_output.txt"
               ORGANIZATION IS LINE SEQUENTIAL.
       DATA DIVISION.
        FILE SECTION.
       FD MY-INPUT-FILE.
       01 MY-INPUT-LINE    PIC X(180).
       FD MY-OUTPUT-FILE.
       01 MY-OUTPUT-LINE   PIC X(181).
       WORKING-STORAGE SECTION.
       01 END-OF-FILE PIC X VALUE SPACE.
          88 END-OF-INPUT-FILE VALUE 'T'.
          88 NOT-END-OF-INPUT-FILE VALUE 'F'.
       
       01 i  pic 9(3) value zero .
       01 j pic 9(3) value 1 .
       01 k pic 9(3) value 1 .
       PROCEDURE DIVISION.
      
         OPEN INPUT MY-INPUT-FILE
         OPEN EXTEND MY-OUTPUT-FILE

         SET NOT-END-OF-INPUT-FILE TO TRUE

         PERFORM UNTIL END-OF-INPUT-FILE
           READ MY-INPUT-FILE
             NOT AT END
             
               PERFORM VARYING i from 1 by 1 until i>180
               If MY-INPUT-LINE(i:1)= SPACE
                 continue
                ELSE
                    MOVE MY-INPUT-LINE(i:1) to MY-OUTPUT-LINE (j:1)
                
               
               END-IF
               END-PERFORM
               WRITE MY-OUTPUT-LINE
             AT END
                SET END-OF-INPUT-FILE TO TRUE
           END-READ
         END-PERFORM
         CLOSE MY-INPUT-FILE
         CLOSE MY-OUTPUT-FILE
         STOP RUN.
I looking for a way to replace the first space with a coma and delete the others , is it possible ?
 
MCM said:
I looking for a way to replace the first space with a coma and delete the others , is it possible ?
Are you meaning somethng like this?

Input:
Code:
foo bar baz spam eggs

Output:
Code:
foo,barbazspameggs
 
yes but with all of them foo bar,baz,spam,eggs ; specially when there are many spaces
 
I don't understand, first you wrote, that you want "to replace the first space with a coma and delete the others" and then, that from "foo bar baz spam eggs" the result should be "foo bar,baz,spam,eggs", where you just leave the first space and replace the others. They seem to be two contradictory things. Please specify the task with some data examples.
 
It was just a typing error , what i want is to replace the first space or the last with a coma :
Code:
exp input : 2400    Xrayonten    Xaviere 2424224THVStreet,e#
            2500 Youngnten    Yannire        25252YongeSStreete#

exp output: 2400,Xrayonten,Xaviere,2424224THVStreet,e#
            2500,Youngnten,Yannire,25252YongeSStreete#
 
The simplest seems to be counting spaces and to set the comma instead of the first space.
 
I used the examples posted in thread: I've extended the basic read/write file example by enhancing the logic similar to that used for skipping spaces with counting spaces.

txt2csv.cbl
Code:
       [COLOR=#804040][b]IDENTIFICATION[/b][/color][COLOR=#804040][b] DIVISION[/b][/color].
       [COLOR=#804040][b]PROGRAM-ID[/b][/color].  TXT2CSV.
         [COLOR=#804040][b]AUTHOR[/b][/color].  MIKROM.

       [COLOR=#804040][b]ENVIRONMENT[/b][/color][COLOR=#804040][b] DIVISION[/b][/color].
       [COLOR=#804040][b]INPUT-OUTPUT[/b][/color][COLOR=#804040][b] SECTION[/b][/color].
       [COLOR=#804040][b]FILE-CONTROL[/b][/color].
       [COLOR=#804040][b]SELECT[/b][/color] MY-INPUT-FILE [COLOR=#804040][b]ASSIGN[/b][/color] [COLOR=#804040][b]TO[/b][/color] [COLOR=#ff00ff]"c:\tmp\cobol\my_input.txt"[/color]
      [COLOR=#6a5acd]         [/color][COLOR=#804040][b]ORGANIZATION[/b][/color] [COLOR=#804040][b]IS[/b][/color] LINE [COLOR=#804040][b]SEQUENTIAL[/b][/color].
       [COLOR=#804040][b]SELECT[/b][/color] MY-OUTPUT-FILE [COLOR=#804040][b]ASSIGN[/b][/color] [COLOR=#804040][b]TO[/b][/color] [COLOR=#ff00ff]"c:\tmp\cobol\my_output.csv"[/color]
      [COLOR=#6a5acd]         [/color][COLOR=#804040][b]ORGANIZATION[/b][/color] [COLOR=#804040][b]IS[/b][/color] LINE [COLOR=#804040][b]SEQUENTIAL[/b][/color].

       [COLOR=#804040][b]DATA[/b][/color][COLOR=#804040][b] DIVISION[/b][/color].
       [COLOR=#804040][b]FILE[/b][/color][COLOR=#804040][b] SECTION[/b][/color].
       [COLOR=#804040][b]FD[/b][/color] MY-INPUT-FILE.
       [COLOR=#2e8b57][b]01 [/b][/color]MY-INPUT-LINE    [COLOR=#804040][b]PIC[/b][/color] X([COLOR=#ff00ff]256[/color]).
       [COLOR=#804040][b]FD[/b][/color] MY-OUTPUT-FILE.
       [COLOR=#2e8b57][b]01 [/b][/color]MY-OUTPUT-LINE   [COLOR=#804040][b]PIC[/b][/color] X([COLOR=#ff00ff]256[/color]).
       [COLOR=#804040][b]WORKING-STORAGE[/b][/color][COLOR=#804040][b] SECTION[/b][/color].
       [COLOR=#2e8b57][b]01 [/b][/color]END-OF-FILE-FLAG [COLOR=#804040][b]PIC[/b][/color] X [COLOR=#804040][b]VALUE[/b][/color] [COLOR=#ff00ff]SPACE[/color].
          [COLOR=#6a5acd]88 [/color]END-OF-INPUT-FILE [COLOR=#804040][b]VALUE[/b][/color] [COLOR=#ff00ff]'T'[/color].
          [COLOR=#6a5acd]88 [/color]NOT-END-OF-INPUT-FILE [COLOR=#804040][b]VALUE[/b][/color] [COLOR=#ff00ff]'F'[/color].
       [COLOR=#2e8b57][b]01 [/b][/color]WS-FIELDS.
          [COLOR=#2e8b57][b]05 [/b][/color]WS-LINE     [COLOR=#804040][b]PIC[/b][/color] X([COLOR=#ff00ff]256[/color]).
          [COLOR=#2e8b57][b]05 [/b][/color]LINE-LENGTH [COLOR=#804040][b]PIC[/b][/color] [COLOR=#ff00ff]9[/color]([COLOR=#ff00ff]3[/color]).
          [COLOR=#2e8b57][b]05 [/b][/color]NR-SPACES   [COLOR=#804040][b]PIC[/b][/color] [COLOR=#ff00ff]9[/color]([COLOR=#ff00ff]3[/color]).
          [COLOR=#2e8b57][b]05 [/b][/color]I           [COLOR=#804040][b]PIC[/b][/color] [COLOR=#ff00ff]9[/color]([COLOR=#ff00ff]3[/color]).
          [COLOR=#2e8b57][b]05 [/b][/color]J           [COLOR=#804040][b]PIC[/b][/color] [COLOR=#ff00ff]9[/color]([COLOR=#ff00ff]3[/color]).

       [COLOR=#804040][b]PROCEDURE[/b][/color][COLOR=#804040][b] DIVISION[/b][/color].
       [COLOR=#804040][b]MAIN-PARA[/b][/color].
         [COLOR=#804040][b]OPEN[/b][/color] [COLOR=#804040][b]INPUT[/b][/color] MY-INPUT-FILE
         [COLOR=#804040][b]OPEN[/b][/color] [COLOR=#804040][b]OUTPUT[/b][/color] MY-OUTPUT-FILE

         [COLOR=#804040][b]SET[/b][/color] NOT-END-OF-INPUT-FILE [COLOR=#804040][b]TO[/b][/color] [COLOR=#804040][b]TRUE[/b][/color]

         [COLOR=#008080]PERFORM[/color] [COLOR=#804040][b]UNTIL[/b][/color] END-OF-INPUT-FILE
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]READ[/b][/color] MY-INPUT-FILE
      [COLOR=#6a5acd]       [/color][COLOR=#804040][b]NOT[/b][/color] [COLOR=#804040][b]AT[/b][/color] [COLOR=#804040][b]END[/b][/color]
      [COLOR=#6a5acd]         [/color][COLOR=#008080]PERFORM[/color] PROCESS-LINE
      [COLOR=#6a5acd]         [/color][COLOR=#804040][b]WRITE[/b][/color] MY-OUTPUT-LINE
      [COLOR=#6a5acd]       [/color][COLOR=#804040][b]AT[/b][/color] [COLOR=#804040][b]END[/b][/color]
      [COLOR=#6a5acd]          [/color][COLOR=#804040][b]SET[/b][/color] END-OF-INPUT-FILE [COLOR=#804040][b]TO[/b][/color] [COLOR=#804040][b]TRUE[/b][/color]
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]END-READ[/b][/color]
         [COLOR=#008080]END-PERFORM[/color]
         [COLOR=#804040][b]CLOSE[/b][/color] MY-INPUT-FILE
         [COLOR=#804040][b]CLOSE[/b][/color] MY-OUTPUT-FILE
         [COLOR=#804040][b]STOP[/b][/color] [COLOR=#6a5acd]RUN[/color].

       [COLOR=#804040][b]PROCESS-LINE[/b][/color].
         [COLOR=#804040][b]INITIALIZE[/b][/color] MY-OUTPUT-LINE
      [COLOR=#0000ff]*  remove leading spaces from INPUT-LINE[/color]
         [COLOR=#804040][b]MOVE[/b][/color] FUNCTION TRIM(MY-INPUT-LINE) [COLOR=#804040][b]TO[/b][/color] WS-LINE
      [COLOR=#0000ff]*  compute length of trimmed line (without trailing spaces)[/color]
         [COLOR=#804040][b]COMPUTE[/b][/color] LINE-LENGTH = FUNCTION [COLOR=#804040][b]LENGTH[/b][/color](FUNCTION TRIM(WS-LINE))
         [COLOR=#804040][b]MOVE[/b][/color] [COLOR=#ff00ff]1[/color] [COLOR=#804040][b]TO[/b][/color] J
         [COLOR=#804040][b]MOVE[/b][/color] [COLOR=#ff00ff]0[/color] [COLOR=#804040][b]TO[/b][/color] NR-SPACES
         [COLOR=#008080]PERFORM[/color] [COLOR=#6a5acd]VARYING[/color] I [COLOR=#804040][b]FROM[/b][/color] [COLOR=#ff00ff]1[/color] [COLOR=#804040][b]BY[/b][/color] [COLOR=#ff00ff]1[/color] [COLOR=#804040][b]UNTIL[/b][/color] I > LINE-LENGTH
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]IF[/b][/color] WS-LINE(I:[COLOR=#ff00ff]1[/color]) = [COLOR=#ff00ff]' '[/color]
      [COLOR=#6a5acd]       [/color][COLOR=#804040][b]ADD[/b][/color] [COLOR=#ff00ff]1[/color] [COLOR=#804040][b]TO[/b][/color] NR-SPACES
      [COLOR=#6a5acd]       [/color][COLOR=#804040][b]IF[/b][/color] NR-SPACES = [COLOR=#ff00ff]1[/color]
      [COLOR=#6a5acd]         [/color][COLOR=#804040][b]MOVE[/b][/color] [COLOR=#ff00ff]','[/color] [COLOR=#804040][b]TO[/b][/color] MY-OUTPUT-LINE(J:[COLOR=#ff00ff]1[/color])
      [COLOR=#6a5acd]         [/color][COLOR=#804040][b]ADD[/b][/color] [COLOR=#ff00ff]1[/color] [COLOR=#804040][b]TO[/b][/color] J
      [COLOR=#6a5acd]       [/color][COLOR=#804040][b]ELSE[/b][/color]
      [COLOR=#6a5acd]         [/color][COLOR=#804040][b]CONTINUE[/b][/color]
      [COLOR=#6a5acd]       [/color][COLOR=#804040][b]END-IF[/b][/color]
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]ELSE[/b][/color]
      [COLOR=#6a5acd]       [/color][COLOR=#804040][b]MOVE[/b][/color] WS-LINE(I:[COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]TO[/b][/color] MY-OUTPUT-LINE(J:[COLOR=#ff00ff]1[/color])
      [COLOR=#6a5acd]       [/color][COLOR=#804040][b]ADD[/b][/color] [COLOR=#ff00ff]1[/color] [COLOR=#804040][b]TO[/b][/color] J
      [COLOR=#0000ff]*      reset spaces counter[/color]
      [COLOR=#6a5acd]       [/color][COLOR=#804040][b]MOVE[/b][/color] [COLOR=#ff00ff]0[/color] [COLOR=#804040][b]TO[/b][/color] NR-SPACES
      [COLOR=#6a5acd]     [/color][COLOR=#804040][b]END-IF[/b][/color]
         [COLOR=#804040][b]END-PERFORM[/b][/color].

From the input file:
my_input.txt
Code:
2400    Xrayonten    Xaviere 2424224THVStreet,e#
            2500 Youngnten    Yannire        25252YongeSStreete#

the program creates this output file:
my_output.csv
Code:
2400,Xrayonten,Xaviere,2424224THVStreet,e#
2500,Youngnten,Yannire,25252YongeSStreete#
 
Too bad he wants to do it in COBOL and not in REXX. It's about 8 lines in REXX.

Frank Clarke
--America's source for adverse opinions since 1943.
 
@MIKROM , thank you very much , if you could clarify something for me :
Code:
   *  remove leading spaces from INPUT-LINE
         MOVE FUNCTION TRIM(MY-INPUT-LINE) TO WS-LINE
      *  compute length of trimmed line (without trailing spaces)
         COMPUTE LINE-LENGTH = FUNCTION LENGTH(FUNCTION TRIM(WS-LINE))

When using the function length was it necessary to put FUNCTION TRIM(WS-LINE instead of just WS-LINE , because it has already the trim data of the input ?
And I also don't understand the need to initialize MY-INPUT-LINE , i thought INITIALIZE was only used to replace characters in a variable .
 
On the data i tested, it was necessary.
For example, this satetment
Code:
MOVE FUNCTION TRIM(MY-INPUT-LINE) TO WS-LINE
makes from the second input line from the file
Code:
2400    Xrayonten    Xaviere 2424224THVStreet,e#
            2500 Youngnten    Yannire        25252YongeSStreete#
this working storage field
Code:
2500 Youngnten    Yannire        25252YongeSStreete#
If your input data comes without leading spaces you don't need it.

The LENGTH without TRIM, i.e.:
Code:
COMPUTE LINE-LENGTH = FUNCTION LENGTH(WS-LINE)
computes the length of the line as defined in working storage, i.e. 256 when defined as
Code:
05 WS-LINE     PIC X(256).
The LENGTH with TRIM, i.e:
Code:
COMPUTE LINE-LENGTH = FUNCTION LENGTH(FUNCTION TRIM(WS-LINE))
computes the length of data contained in the field without leading and trailing spaces, for example for
Code:
2500 Youngnten    Yannire        25252YongeSStreete#
it computes 52

The best way to see what the specific statement causes is to comment it out or modify it and try to run the program to see what happens.
 
OK , thank you for your help.
 
HELLO ,
Code:
 INITIALIZE MY-OUTPUT-LINE
      *  remove leading spaces from INPUT-LINE
         MOVE FUNCTION TRIM(MY-INPUT-LINE) TO WS-LINE
         MOVE FUNCTION TRIM(LEADING zeros FROM MY-INPUT-LINE) to WS-LINE

I tried adding the third line to remove leading zeros from another file i have but it gives an error with "LEADING" , I researched the syntax for Trim function and it seems to be wright .

Code:
 000000000100 Anderson                    Adrian              1113 Peachtree Plaza, Suite 111


I tried using the unstring function but it also doesnt work .
 

This is the web site i went to ., after that i found that excel does it automatically for csv data , sorry to have wasted your time .
But the problem i found is that i want comas between more than 2 space i.e the columns of my txt file . I tried to do it with a 3rd index but i havnt found the solution yet .
 
The link you provided is not for COBOL but for an IBM product IBM App Connect.

You can remove the 8 leading zeros as follows:
Code:
MOVE '000000000100' TO WS-CHAR-12
DISPLAY 'With Leading Zeros:'
DISPLAY WS-CHAR-12

MOVE FUNCTION SUBSTITUTE(WS-CHAR-12,'00000000', SPACE) 
     TO WS-CHAR-12
DISPLAY 'With 8 Leading Zeros substituted with space:'
DISPLAY WS-CHAR-12

MOVE FUNCTION TRIM(WS-CHAR-12 LEADING) 
     TO WS-CHAR-12
DISPLAY 'With leading space trimmed:'                 
DISPLAY WS-CHAR-12

Output:
Code:
With Leading Zeros:
000000000100
With 8 Leading Zeros substituted with space:
 0100       
With leading space trimmed:
0100
 
OK , Thank you for your help , sorry for the delay of the answer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top