Posted: Tue Sep 08, 2020 12:09 pm Post subject: Find replace duplicate apostrophe in a varying length string
Need help. There is a need to replace duplicate double quotes (apostrophe) in a varying length string in a FB file having LRECL as 133. Here's the example
Input:
Code:
05,09082020,1234536666,"'ABC,DEF'",123456-,123456,123456789,0,"JOHN CARPENTER : "FIRST TEXT" END STRING",12,""GHIJKL""
Desired Output:
Code:
05,09082020,1234536666,"'ABC,DEF'",123456-,123456,123456789,0,"JOHN CARPENTER : 'FIRST TEXT' END STRING",12,"'GHIJKL'"
This is a comma separated file and the length of each field in the record varies. If you look closer, I need to replace the duplicate Apostrophe with single quote in the fields.
1. First duplicate
From
Code:
"JOHN CARPENTER : "FIRST TEXT" END STRING"
To
Code:
"JOHN CARPENTER : 'FIRST TEXT' END STRING"
2. Second duplicate
From
Code:
""GHIJKL""
To
Code:
"'GHIJKL'"
I tried thinking to use PARSE, IFTHEN , FINDREP but couldn't get closer. I did search in the forum to the best possible extent and couldn't find a closing match. Can you please help?
Thanks Srihari Kolusu very much. The inner double quotes are not always followed by a space, or in other words there is no known pattern. If I had to write a COBOL code, the my logic would be to take the length of each string and then omit the first and last apostrophe and replace any apostrophe in between to a single quote. Can this be done in DFSORT?
Joined: 26 Nov 2002 Posts: 12375 Topics: 75 Location: San Jose
Posted: Tue Sep 08, 2020 1:34 pm Post subject:
nachiyappan wrote:
Thanks Srihari Kolusu very much. The inner double quotes are not always followed by a space, or in other words there is no known pattern. If I had to write a COBOL code, the my logic would be to take the length of each string and then omit the first and last apostrophe and replace any apostrophe in between to a single quote. Can this be done in DFSORT?
Well we can PARSE and remove the Double quotes with the string. From your sample you have 12 parsed fields. So if you can let us know the fields that needs the double quotes to removed, then we can handle that.
btw this field does NOT make any sense at all
Code:
"'ABC,DEF'",
If the above field is a comma separated field, why does it have quotes as if it is a single field. Are they 2 different fields with mismatched quotes?
is one field and is a name field with first and last name separated by a comma. The comma in there is not part of the CSV formatting and hence included in Apostrophe.
The fields that need the duplicate double quotes to be removed are 9 and 11. Thank you!
Hi Kolusu, for the example I provided , this approach worked really well. Thank a lot for providing this valuable solution. I have another challenge. If any fields are blank in the subsequent records, then they are getting omitted, but I want to keep them intact. Here's the example for this situation
Input:
Code:
05,09082020,1234536666,"'ABC,DEF'",123456-,123456,123456789,0,"JOHN CARPENTER : "FIRST TEXT" END STRING",12,""GHIJKL""
05,09082020,1234536666,"'ABC,DEF'",,,123456789,0,"JOHN CARPENTER : "FIRST TEXT" END STRING",12,""GHIJKL""
Output:
Code:
05,09082020,1234536666,"'ABC,DEF'",123456-,123456,123456789,0,"JOHN CARPENTER : 'FIRST TEXT' END STRING",12,"'GHIJKL'"
05,09082020,1234536666,"'ABC,DEF'",123456789,0,"JOHN CARPENTER : 'FIRST TEXT' END STRING",12,"'GHIJKL'"
the desired output is
Code:
05,09082020,1234536666,"'ABC,DEF'",123456-,123456,123456789,0,"JOHN CARPENTER : 'FIRST TEXT' END STRING",12,"'GHIJKL'"
05,09082020,1234536666,"'ABC,DEF'",,,123456789,0,"JOHN CARPENTER : 'FIRST TEXT' END STRING",12,"'GHIJKL'"
The 5th and 6th columns had blank in the input which got omitted in the output but I wanted to keep that intact. Can this be done? Thanks much in advance.
Now coming to your new requirement, it is easy to handle. In the parse itself we can add a lead character to identify the empty strings. I used '#' to tag the empty fields.
Hi Kolusu,
In fact, I tried this already and really glad that this worked out. The work which may span into multiple days of development definitely shortened because of your help. Thank you very much.
I will keep note to use square brackets for code tags going forward.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum