1.

Solve : string parsing in a textfile (Batch solution)?

Answer»

Hello,

I have have a huge textfile like this: (wget from a php SITE.)


number:name1:name2:orga:phone
number:name1:name2:orga:phone
....

I NEED it as a list like this:

number:name1:name2:orga:phone
number:name1:name2:orga:phone

How can I replace
through a return?

I am looking for a Batch SOLUTION.
(Using a Webbrowser to view and copy and paste is not an option.)

thanks in advance for any help
uli

Uli,

Batch parsing only recognizes single character delimiters. This little script might help you out:

Code: [Select]Const ForReading = 1
Const ForWriting = 2
filespec = "c:\filename.log" ' <== fully qualified name of your file

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filespec, ForReading)
strData = f.ReadAll
f.Close
set f = fso.OpenTextFile(filespec, ForWriting)
strArray = Split(strData, "<BR>")
For x = 0 to UBound(strArray) - 1
f.Write strArray(x) & vbCRLF
Next

Save script with a VBS extension. You can LAUNCH the script from any batch file by including this line:

call cscript scriptname.vbs

Good luck. 8-)Nice Post Sidewinder. In addition to his great post if you're familiar with PHP you could also create a PHP script using some regular expressions to parse the file and do this for you. Unfortunately I'm more of a Perl man than a PHP man so not that familiar with PHP, but I believe the syntax is very close, espcially when it comes to regular expressions.

Something like the below example may work.
Code: [Select]#Get file into array
open (MYFILE, "<file.txt");
@myfile = <MYFILE>;
close (MYFILE);

$myfile = "@myfile";

#regular expression to replace the <br> tags with a new line.
$myfile =~ s/<br>/\n/g;

#Save output to new file.
open (MYFILE, ">newfile.txt");
print MYFILE "$myfile";
close (MYFILE);


In reguards to a .bat or batch file doing this, parsing and handling data is not something a batch file is really meant to do.It looks like text parsing is possible within a batch file (assuming you use a third-party utility). If you're really wanting to do this through a batch file you may want to use SED.

http://www.student.northpark.edu/pemente/sed/index.htmMany thanks for help.

Sidewinders solution works perfect.

I tried sed before and didn´t really understand how it works.

uli



hi
just curious, anybody uses edlin before. i suppose it has a search and replace function. am not sure whether it is too tedious or even possible to use it to "change"
to newline....
anyway, just wondering.....Oh, I didn´t think of edlin.
I never used it.

uliUgh edlin. It's been so long since I've used it I can't RECALL and honestly think it's best that I keep it forgotten. However, another good possible solution. I solved the problem with the carriage return. 8-) 8-)
Looks a bit strange cause HTML Tags have to be double quoted in variables.

set var="
number:name1:name2
number:name1:name2
.... "
set var=%var:
=#~#%
set var=%var:"=%
set var=%var:#~#=^&echo.%
echo %var%

cheers
uliI like Uli's trick replacing the
with &echo.. To avoid the double quotes and the strange code needed to work around the workaround parse the file directly into a variable using the FOR command:

Code: [Select]for /f "tokens=*" %%a in (textfile.txt) do set var=%%a
set var=%var:<br>=^&echo.%
echo.%var%I'm just afraid that this will not work for large files since the size of a variable in DOS may be limited. :-/



Discussion

No Comment Found