| 1. |
Solve : Control Characters in File - Help? |
|
Answer» I'm trying to write a batch file to replace certain data in a file. Here is approximately what the data looks like. I need to copy the data to another file, then in the in the copy - replace the XXX in positions 2-5 with ZZZ. Then merge the data together so I have a combined file with rows with both the XXX and ZZZ. Text files can not be updated in place. Better to do a search & replace against the original file and output the changes to a new file. Quote copy source.dat+work.dat Seems like a incomplete statement. When I tried it, just got a copy of the changed file. You can always use the DEL command to cleanup any work files. Quote At the end, I end up with something that looks like this: What did you use to view the file? And just out of curiosity, what program created this file? Thanks for your response. I tried the set input then found it could be possible to have the "X's in other places in the file besides position 2-5 so I can't do a global replace on every instance of "X's" only that in position 2-5. The statement that I have is working to just replace the "X's" with ZZZ in 2-5. The main issue is the rows with the &. It's truncating data after the & as its writing the line to the output file. The other lines are ok. I'm trying to feed this file into another application so I can't have the carriage-return-line-feed at the end of the line. The copy statement that I'm using has a folder structure in it that specifies where the files are. I'm using XP - this is the statement: and its working ok. I just get the control character at the end after the copy and its not in either of the files prior to the copy so I know its the copy statement that is putting it there. Not sure if there is an option to add to the copy to AVOID - I couldn't find one. copy c:\RESIQ2\source.dat+c:\RESIQ2\WORK.dat The actual file that has this data can be hundreds of lines long which is why we're trying to automate. The manual search and replace is very prone to error. The file was created out of a banking program - I'm not sure of the exact software. I'm open to another option besides dos batch. It just can't require elaborate setup or installation of other software as this has to run on the users desktop. Again thanks. If you can think of anything else, let me know.Quote If you can think of anything else, let me know. We can always think of something else. If you want to consider VBScript, we could probably whip something up for you. Does each line have a carriage-return line-feed? It's easier if each line has a beginning and an end. [mind drift] Back in the day there was a VM editor called Xedit that could easily gang punch columns of data. It was ported to the PC as The (). It could be scripted with REXX and was a godsend when working with files. [/mind drift] you can use vbscript. or you can use gawk ( if you can) . download and install from here. save the code below as script.awk Code: [Select]{ print $0 gsub(/&|\$/,"") #remove & and $. a[++c]=substr($0,1,1)"ZZZ"substr($0,5) } END { for (i=1;i<=c;i++) { print a[i] } } on command line Code: [Select]C:\test>more test.txt LXXX0220451 0337 BEN:QUALEX KS& N LXXX0220451 0337 TOTAL#OFITEMSI N LXXX0220451 0337 L-C DIVISION O N LXXX0220451 0337 2642, DEPART 4 N LXXX0220451 0337 BEN_ /& TOT$ N C:\test>gawk -f change.awk test.txt LXXX0220451 0337 BEN:QUALEX KS& N LXXX0220451 0337 TOTAL#OFITEMSI N LXXX0220451 0337 L-C DIVISION O N LXXX0220451 0337 2642, DEPART 4 N LXXX0220451 0337 BEN_ /& TOT$ N LZZZ0220451 0337 BEN:QUALEX KS N LZZZ0220451 0337 TOTAL#OFITEMSI N LZZZ0220451 0337 L-C DIVISION O N LZZZ0220451 0337 2642, DEPART 4 N LZZZ0220451 0337 BEN_ / TOT N |
|