Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Loop through a file, check for specific characters and replace. Use new abap

former_member201275
Active Contributor
0 Kudos

Hi all,

I would like to loop through a file (open dataset, read dataset, etc.), check for certain characters in the rows of this file, and replace where necessary. I know how to do this but I still use the old abap code, is there a neater, newer, better, way of doing this with abap 7.40?

Here is a sample of the code I have. There are more characters that I have to test for but this is just a small example to demonstrate my point:

        IF gv_string CS 'E#'.
          REPLACE 'E#' WITH 'n'.
        ELSEIF gv_string CS 'M<'.
          REPLACE 'M<' WITH 'la'.
        ELSEIF gv_string CS 'T#'.
          REPLACE 'T#' WITH 'abc'.
        ELSEIF gv_string CS 'XZ'.
          REPLACE 'XZ' WITH 'w'.
        ENDIF.

All help, tips, etc., are really greatly appreciated.

I wish you all a nice day further.

1 ACCEPTED SOLUTION

raymond_giuseppi
Active Contributor
0 Kudos

Did you look at SAP tools such as FM SCP_CHECK_CHARSET_OF_STRING to replace such "weird" characters.

Import Parameters                Value

 TEXT                            Józef
 LANGU                           EN
 BLENDED_JAPANESE                X

Export Parameters                Value
 IS_OK
 WRONG_POS                       1
 METHOD                          English special with code page 1101,1155
 PROPOSAL                        Jozef
 PROPOSAL_OVERFLOW

Or call SCP_REPLACE_STRANGE_CHARS directly

(Look in your system for more up to date tools)

9 REPLIES 9

FredericGirod
Active Contributor
0 Kudos

I never understand why everybody try to do job need to be done at OS level. Ask the basis administrator to create shell script using SED command line.

VXLozano
Active Contributor

Just wondering... if you find the E# string, you want NOT to replace the M< ones?

former_member660513
Participant
0 Kudos

Whether you do it in Perl, SED, AWK, or any tool or language(ABAP?), you need some kind of a regex engine, don't you? So what could be new here in 7.40 and upwards?

Maybe string templates, in case you don't use them yet - for example this blog, even though it is old

https://blogs.sap.com/2013/05/17/using-new-abap-stuff-new-options-for-strings/

VXLozano
Active Contributor
0 Kudos

cl_gui_frontend_services:

  • GUI_UPLOAD to get the content,
  • GUI_DOWNLOAD to save the new content

So, you will have your file in a table, you pour the content of the table into a string and use the TRANSLATE function or command, or the REPLACE sentence, the one who fits your needs the most. Then you split the new string into another table and download it as your new file.

Not sure if you can TRANSLATE pairs, so maybe REPLACE will be easier.

VXLozano
Active Contributor
0 Kudos

BTW, you don't need to check if the string contains the substring you want to replace... just call the REPLACE thing, and if the string doesn't contains the chain to replace, sy-subrc will just change its value to non-zero.

Sandra_Rossi
Active Contributor
0 Kudos

Sorry, I don't understand the code at all. I wouldn't recommend anything before understanding the goal of these replacements. Probably the code around must be rewritten too (and around the program too?) and maybe that will make the REPLACE useless.

former_member201275
Active Contributor
0 Kudos

I am really sorry my questions are not good, I am not very bright.

I have a frontend website which contains a list of persons names plus some additional information on them, for example address and email. These are populated by a file which resides in SAP. Every night a job runs and updates the information from the SAP backend to this url.

The file in SAP however sometimes has strange characters, for example for a person’s name we have, 'JE#zef'. At least in the sap gui this is what i see. On notepad++, for the same file I see 'JEEOTzef'. When the nightly job runs to update the frontend, these people are left out, because of the irregular characters.

This is because the persons actual name is 'Józef'. It is a polish name.

In this person’s case I want to replace the ‘E#’ with a normal ‘o’. The reason for this is that in their email address they have an ‘o’ and this is close to the ó. If I could do this, then when the job runs at night, the website would then show the person’s name as ‘Jozef’, and this is what I want as we cannot work with ó.

I do not have the option to go back a step earlier to where the file is delivered to our sap system. I have only the option to work with the file now as it is, and on our frontend i.e. on the website we want to have 'Jozef'.

As a second task, I want to create a separate program which proofreads files on the SAP server. This program should read a file, from AL11, and look for characters in this file which cannot be converted to Unicode. When I find a file with at least one character which cannot be converted to unicode I will then report this.

Sandra_Rossi
Active Contributor

You know that computers store characters only with 0 and 1. Based on what "code page" (or "character set") and transformation algorithm a human decides a program to encode a given character, this character may be encoded into different 0 and 1. If it's in Unicode UTF-8, ó (known as Unicode character U+00F3) is encoded into two bytes with values 11000011 and 10110011 (hexadecimal C3 and B3). If another program reads that but doesn't know that it's UTF-8 encoding, it may understand something completely different.

Do as you wish/as you can 😉

raymond_giuseppi
Active Contributor
0 Kudos

Did you look at SAP tools such as FM SCP_CHECK_CHARSET_OF_STRING to replace such "weird" characters.

Import Parameters                Value

 TEXT                            Józef
 LANGU                           EN
 BLENDED_JAPANESE                X

Export Parameters                Value
 IS_OK
 WRONG_POS                       1
 METHOD                          English special with code page 1101,1155
 PROPOSAL                        Jozef
 PROPOSAL_OVERFLOW

Or call SCP_REPLACE_STRANGE_CHARS directly

(Look in your system for more up to date tools)