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 Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Manipulating long text lines, similar to fields in a table 3

Status
Not open for further replies.

Don Child

Programmer
Apr 4, 2003
65
US
Hi,

Normally if I have a text file with line lengths less than 255, I can APPEND the text file into a table, manipulate the lines in the table, then output the changes to another text file.

But the text files I'm working with, have line lengths of around 1000 characters. The line length is uniform throughout the text file.

Is there a way to manipulate the text file, like e.g. REPLACing the minuses in the BBB line types with pluses, and replacing the pluses with minuses (essentially reversing all the amounts in the text file)?

I'm trying to do it from the REPL/DOT PROMPT, rather than writing a script.

If it's necessary, I'll write the script, and probably populate an array with the text file, then loop through it and do the replace. I just wanted to see if it's possible to do this manipulation manually.


AAA FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE FIRSTLINE
BBB -0000123.00ABCD
BBB +0000777.00ABCD
BBB +0000845.00ABCD
CCC HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
DDD 39292983987AABBBBCCCCC
 
Hi,

You may want to have a look at the MEMO field type, the EDITBOX control and the functions ALINES(), ATLINE(), MLINE() ...

hth

MarK
 
You can read a file into a variable with FILETOSTR(), then manipulate that variable with STRTRAN() and then save it with STRTOFILE().
Since you want to replace quite near to the line start, this could work like this:

Code:
lcFileName = GETFILE()
lcFile = FILETOSTR(lcFileName)
STRTRAN(lcFile,CHR(13)+CHR(10)+'BBB +',CHR(13)+CHR(10)+'BBB -')
STRTRAN(lcFile,CHR(13)+CHR(10)+'BBB -',CHR(13)+CHR(10)+'BBB +')
STRTOFILE(lcFile,lcFilename)

I took 'BBB' literally in this case. If it varies what's before the first sign of a number or there are further numbers later in the lines, it's recommendable to do something in the sense of fully understanding the file format and read it into a table with the proper structure of multiple fields.

Chriss
 
You could also prepare a DBF with multiple char fields, for example five C(254) fields, to use in such occasions and then APPEND FROM (GETFILE()) TYPE SDF.


Chriss
 
If each line consists of a number of logical fields, and if those fields are of uniform length throught the file, you could use [tt]APPEND FROM ... TYPE SDF[/tt]. The receiving table would need to have fields that match (in length) the fields within the line of text. If that's the case, the conversion is more or less automatic.

The only minor snag is that date fields are not always converted correctly. See the Help topic for APPEND FROM for details.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hi mjcmkrsr and Chris,

Yep, I figured filetostr would be necessary, but couldn't figure out how we could perform something similar to table manipulations. I would still have to specify the exact location on each line where the specific pluses and minuses are, since that might be other occurrences of these characters that shouldn't be negated.



Chris and Mike,

Yes, I think that's the answer, to just split the table into multiple fields that would contain the full string, SDF append, and then do something like

REPLACE TEXTLIN1 WITH LEFT(TEXTLIN1, 100 ) + "+" + SUBSTR(TEXTLIN1,102) FOR LEFT(TEXTLIN1,3) = "BBB" .AND. SUBSTR(TEXTLIN1,101,1) = "-"

REPLACE TEXTLIN1 WITH LEFT(TEXTLIN1, 100 ) + "-" + SUBSTR(TEXTLIN1,102) FOR LEFT(TEXTLIN1,3) = "BBB" .AND. SUBSTR(TEXTLIN1,101,1) = "+"

(Not tested yet)

Obviously, I have to figure a way to avoid REPLACing lines with pluses that were already processed in the first REPLACE. But at least we have the basic idea.


Sounds good, thanks.


Regards,

 
If you know the numeric data has its sign at position 100 of a line, you can design your import DBF to read in the 99 characters before it into field1, then the + or - would be the first character of field2.

And, not only can you read in anything longer than 255 chars into as many char(254) fields as necessary but a COPY TO filename TYPE SDF also outputs the field concatenated without adding commas or other separators.

Chriss
 
Chris,

Thanks, the Xbase designers thought of everything.

 
Hi,

or you use a three step approach

Code:
cTest = "hello - and + world"
? cTest
cTest =  CHRTRAN(cTest, "-", "*")
? cTest
cTest =  CHRTRAN(cTest, "+", "-")
? cTest
cTest =  CHRTRAN(cTest, "*", "+")
? cTest

hth

MarK
 
Yes, that's it. That's the final piece, an intermediary Search & Replace.

Thanks, mjcmkrsr.

 
With CHRTRAN(cFilecontent,'+-','-+') you also get all signs inverted without needing *. And the approach with * would also need to ensure there are no * in the original you then turn into +.

If you read in by APPEND, you have separate lines, no matter how the fields are composed, so you only ever have a single + you turn into a - or a single - you turn into a +, so no need for any trickery, here, especially not one that turns an initial * into +, when they should stay *.

Since you know the column of the signs that interests ou, I'd rather use the APPEND approach.

Chriss
 
It surely fits the example in the first post, as the positive numbers have a + sign (quite unusual).
Then you don't need APPEND. just a FILETOSTR() and STRTOFILE(). On the other side, the familiarity with the APPEND approach can be used and continued, just using multiple char fields and the SDF file type. Now you can pick whatever you prefer.

Chriss
 
Hi Chris,

I'm already settled on doing APPEND, because then I can in effect manipulate the text file, the same way as a table.

With the FiletoStr, StrToFile, I'd end up with a memo field that contains the entire file.

I didn't know about CHRTRAN. That's a nice shortcut, thanks.
 
All working perfectly, thanks everyone.


LIST SubStr( D2LINE1, 87, 1), ChrTran( SubStr( D2LINE1, 87, 1), "+-", "-+" ) FOR SUBSTR( D2LINE1, 72, 3 ) = "BBB" to file C:\DOWNLOAD\REPL.TXT

SUBSTR(d2line1,87,1) CHRTRAN(SUBSTR(d2line1,87,1),"+-","-+")
- +
+ -
+ -
+ -
+ -
...
...
...
+ -
+ -
+ -
- +
+ -
+ -
+ -
+ -
+ -
+ -
...
...
...
+ -
+ -
+ -



REPLACE D2LINE1 WITH LEFT(D2LINE1,86) + ChrTran( SubStr( D2LINE1, 87, 1), "+-", "-+" ) + SUBSTR(D2LINE1, 88, 166 ) FOR SUBSTR( D2LINE1, 72, 3 ) = "BBB"

*** Confirmed reversed signs in the table ***

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top