Click here to Skip to main content
15,867,780 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am having an issue with a txt file. I need to edit it so that any time the file sees 16,451 it replaces that to 16,475

Additionally I need to replace the underlined 0 with the bolded numbers.

I am having problems because sometimes that data is all in one line and sometimes it is in two lines.

Once I've figured out a way to do this, I need to do it to 220 files. Any and all help is much appreciated.


Original File:
16,451,13039,,42,0,PAY PAY SERVICE REF*CK*6416*Peoples Gas
49,2460
98,24607
02,104
03,13100
16,451,16228,,42,0/
88,PAY PAY SERVICE REF*CK*6414*Commonwealth Edison
16,451,123750,,42,0/
88,PAY PAY SERVICE REF*CK*6415*Leasing
49,2799
98,27995

What I need:
16,475,13039,,42,6416,PAY PAY SERVICE REF*CK*6416*Peoples Gas
49,2460
98,24607
02,104
03,13100
16,475,16228,,42,6414/
88,PAY PAY SERVICE REF*CK*6414*Commonwealth Edison
16,475,123750,,42,6415/
88,PAY PAY SERVICE REF*CK*6415*Leasing
49,2799
98,27995

What I have tried:

I've tried some smaller things, but nothing I know of is working.
Posted
Updated 25-Jun-20 21:02pm
Comments
Garth J Lancaster 26-Jun-20 3:41am    
I see you've updated it - I might have missed that the data for this 16,475,16228,,42,6414 comes from the line 'below it' ie an '88' line ... not impossible to account for, depending on how many lines are in the files on average

the basic idea, as OriginalGriff Said, use a CSV reader, or even string split

You haven't stated a programming language - anyone for awk ? nor have you stated how many lines the file(s) have (or is that it as shown)

The logic 'looks like' (tm, not proven)

1) Pick a language you're comfortable
2) Get a list of the required files - by name or 'in a directory'
3) for each file, open a new output file, open the (input) file and read it line by line
4) if the line starts with '16,', check for the two conditions below, else write it verbatim to the output file
5) 'split' the line based on the ','
6) 1st condition : assuming a zero/0 based offset, If the line has element[0] == 16 AND element [1] == 451 AND Number of Elements == 6, and element [5] != '0', Split element [5] by the '*' and hold element [2].. this is the 6416 from the first line
7) You can then set element [1] of the line array to '475' and element [5] to element [2] from the second split from point 6, and write that array to a new file using a 'join' and ',' with a C/R terminator of course
8) 2nd Condition : If the line has element[0] == 16 AND element [1] == 451 AND Number of Elements == 6, and element [5] == '0', you replace elements [1] & [5], element [1] with '475' and element [5] with the stored 2nd split value eg '6416', and write that array to a new file using a 'join' and ',' with a C/R terminator of course
9) Close the file (or in C# use 'using blocks' around input and output files)

[Edit] To make your program easier, build it in stages, eg
Stage 1 : get a list of the files you need to process, loop & display to 'console'
Stage 2 : use the first file - open it, read it line by line, write it untouched line by line to a new file
Stage 3 : Modify Stage 2 to do the edits - this way you can be sure everything before works
[/Edit]
 
Share this answer
 
v2
Comments
Maciej Los 26-Jun-20 3:03am    
5ed!
Patrick07 29-Jun-20 17:49pm    
So each of these are transactions. If the transaction doesn't fit on one line it will start the next line with 88, (the rest of the transaction). Sometimes the transaction is all on one line and sometimes it is on two lines. Is it possible to build something that can detect that? How much would it cost for someone to code something to do this?
Garth J Lancaster 29-Jun-20 19:29pm    
I think you miss the point of CodeProject - we aren't a coder for hire site - there are plenty of those out there - we help people write code ... but even if you went to a coder-for-hire, you would have to supply proper 'requirements' with an analysis of your data, which you havent done well here - you could have put that comment about the '88' in your question for example and made life easier.

You havent answered the question I asked you about how large/many lines there are in the files - another vital piece of the information that could make the cost cheap or expensive

My answer to this question would be 'yes', it could be done, but could cost a 'a lot' ..

Is there a name for this data format ? (or, where does it come from - North America Utility companies ??)
Where are the files ? all in one directory or scattered ?
What do the filenames look like ?
How large are the files/how many lines are in them ?
There is no automatic way to do that, and the data you show doesn't provide enough information to produce a reliable app to do what you want: several of your bold numbers have no obvious "0" to replace. a "blind replacement" of "16.451" with "16.475" is also likely to produce errors as it can be a substring of a different value: "116.451" for example.

So you need to start by working out what the rules are - or you will mess up a considerable amount of data, probably beyond hope of recovery.

Then, I'd use a CSV reader library, and write a relatively simple app to change the data.
This may help: A Fast CSV Reader[^]
 
Share this answer
 
Remarks:

1.
Quote:
any time the file sees 16,451 it replaces that to 16,475

As long as ',' (comma) will be used as a delimiter, any parser will return two numbers: 16 and 451 instead of one: 16,451.

2.
Quote:
I need to replace the underlined 0 with the bolded numbers

Text file does have none information about text formatting! So, even if you add html tags into it, it will display zero surrounded with html tags: <b>0</b>. To display formatted text you'll be in need to convert text file into html or rtf file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900