1.

Solve : Changing delimiter in a text file.?

Answer»

Hi All

I currently have a text file in the format of:

"Name","Customer Document","{Sequence Number}","773","{Batch Class Name}","Name","{Document FORM Name}","Bank Code","Customer Number","11111","Customer Name","ABC","{Scan Operator's Station ID}","Highbury01","{Batch Creation Date}","2/13/2009","{Batch Creation Time}","10:48:54 AM","\\Images\00000190.TIF"

where each value is delimited by a comma(,).

How would i write a batch file which is going to acess this .txt file on a daily basis, and convert all comma (,) values into pipe ). so it will look as follows:

"Name"|"Customer Document"|"{Sequence Number}"|"773"|"{Batch Class Name}"|"Name"|"{Document Form Name}"|"Bank Code"|"Customer Number"|"11111"|"Customer Name"|"ABC"|"{Scan Operator's Station ID}"|"Highbury01"|"{Batch Creation Date}"|"2/13/2009"|"{Batch Creation Time}"|"10:48:54 AM"|"\\Images\00000190.TIF"

I know i can find and replace in notepad but i'd rather have a batch file created and added to a schedule so that there is no human intervention.few ways
1) vbscript
Code: [Select]Set objFS = CreateObject("Scripting.FileSystemObject")
Set d = CreateObject("Scripting.Dictionary")
strFile = "C:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile,1)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
WScript.Echo Replace(strLine,",","|")
Loop

2) specific text processing tools like sed and (g)awk will do the job. these tools can also be found in Microsoft Services for Unix (if you have it).
Code: [Select]C:\test>sed "s/,/|/g" file.txt
"Name"|"Customer Document"|"{Sequence Number}"|"773"|"{Batch Class Name}"|"Name"|"{Document Form Name}"|"Bank Code"|"Cus
tomer Number"|"11111"|"Customer Name"|"ABC"|"{Scan Operator's Station ID}"|"Highbury01"|"{Batch Creation Date}"|"2/13/20
09"|"{Batch Creation Time}"|"10:48:54 AM"|"\\Images\00000190.TIF"

3) Programming languages, eg Python or Perl as well as others.
eg Python
Code: [Select]for line in open("file.txt"):
print line.strip().replace(",","|")
Can there EVER be a comma within a value?Thanks Ghost. well let me be more specific.

I need to search the text file for commas (most likely using the find command) and replace it with a pipe. from my research, the only command is MUNGE.exe which i have to download but i am not too keen on. So pretty much, every night, the schedule is going to execute the batch file which inturn will search the specific text file for commas and replace it with a pipe.

VB is not my strong point so i want to avoid it as much as possible.

Is there any other wayHey BC_Programmer, no, the values that are being written into the text file are being taken out of DB2 and we have ensured that those values do not contain any comma'she gave you three different ways...ghostdog74,

sed "s/,/|/g" file.txt worked like a charm.

The sed download for windows was quick and easy.


You guys provide solutions so quick that the original poster fails to see what you have done.

It looks like the people requesting help would provide more feedback.

The unix commands are much better than DOS Batch or DOS commands.

What is the best download site for the other unix commands that will work with windows?

Bill

well- there is unxutils, found here:

http://unxutils.sourceforge.net/

and I found this:

http://lifehacker.com/362316/use-unix-commands-in-windows-built+in-command-prompt


I'm sure ghostdog can shed some more light on this... I haven't really used linux (except for a few recoveries)Quote from: BC_Programmer on March 19, 2009, 08:56:42 AM

well- there is unxutils, found here:

http://unxutils.sourceforge.net/

and I found this:

http://lifehacker.com/362316/use-unix-commands-in-windows-built+in-command-prompt


I'm sure ghostdog can shed some more light on this... I haven't really used linux (except for a few recoveries)


1) Microsoft services for unix
2) Cygwin and
3) GNU for Windows

I use the Core Utils

http://gnuwin32.sourceforge.net/packages/coreutils.htmnot to forget these tools also
1) FindUtils for windows , the find command much better than WINDOW's version
2) DiffUtils , for comparing files and such.
3) Gawk, Sed as mentioned.
4) Grep.

For a full list of available unix commands for Windows, check out this directory


Discussion

No Comment Found