Application Development and Automation 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: 
Read only

loop and parse a text file

Former Member
0 Likes
1,077

Hi,

I have 3 different text files that are tab delimited. My problem is that data for each instance could be spread over 2 or more different text lines, such as a company name and address

Example:

ABC Co.

1234 My Dr.

Anywhere, VA 12345

DEF Co.

5678 My Dr.

Anywhere, VA 67890

Is there a way to just open the file, loop through all lines, and parse out the relevant data to put into internal tables? Get Company name, Street Address, City, State, Zip? Basically the files are reports from some program that export out to text file format. The have header records and have page breaks that causes some data to be spread out over 2 pages.

I don't think the GUI_UPLOAD & GUI_DOWNLOAD will help in my case

Thanks for any help or suggestions.

5 REPLIES 5
Read only

Clemenss
Active Contributor
0 Likes
774

Hi Michael,

it will imply some errors if you do not have any record type for the different text lines. What I see in your example ist 3 text lines per company followed by 1 space line (?).

You could use this as a rough pattern: After a blank line comes the company name. next line contains street No folloewd by street name. Next line is city name folowed by comma, followed by state abbreviation followed by postal code. Use patterns to verify/identify line content; i.e. line starting with digits is for street, line containing comma is city etc.

Try to identify the pattern structure of your input file and do some tests.

AFAIK SAP does not offer any standard method for this.

Regards,

Clemens

Read only

Former Member
0 Likes
774

could you explain the record type functionality a little further?

Read only

Clemenss
Active Contributor
0 Likes
774

Hi Michael,

in file transfer it is a common method to use the first 1-3 characters of a record to distinguish the record structure; i.e. '01' for header, '02' for item and '03' for trailer or consistency check. Or in your case '01' for company name, '02' street, '03' state or whatever.

Each record type defines/expects it's own structure. Using this you can parse the file an handle the records according to their type.

For each record processed you would use a case statement for the record types.

But a precondition for this is that the exporting program creates the record type prefix.

Hope you got what I'm talking about.

Regards,

Clemens

Read only

pradeep_nellore
Participant
0 Likes
774

Hi,

If u know exactly the number of fields u can solve this problem i guess.

keep a counter variable for counting the fields moved to ur internal table.

Try the following code.

loop at itab.

move fields to internal table work area.

if count = 7.

append.

endif.

endloop.

Reward if helpful.

Bye.

Read only

Former Member
0 Likes
774

ok now i can open a file and loop using this:

OPEN DATASET v_fname FOR INPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc = 0.

DO.

READ DATASET v_fname INTO srow.

IF sy-subrc <> 0.

EXIT.

ENDIF.

WRITE: / srow.

ENDDO.

ENDIF.

CLOSE DATASET v_fname.

now I have sRow equal to lines like this:

DATE DOCUMENT VENDORID NAME GROSS AMT DISCOUNT NET CHECK

-


04/26/07 40012081 COFFEE COFFEE HOUSE 56.00 0.00 56.00

I want to be able to parse out each field into their own variables for later use. Split probably will NOT work in this case because the only character to split would be a blank space, but that would split the vendor name and that can't happen. Is there a string function that goes like this:

var1 = substring(sRow, 1, 😎 '04/26/07

var2 = substring(sRow, 9, 😎 '40012081

var3 = substring(sRow, 12, 10) 'COFFEE

var4 = substring(sRow, 23, 15) 'COFFEE HOUSE

var5 = substring(sRow, 39, 😎 ' 56.00

etc.

thanks for all the help so far