Computing.Net > Forums > Programming > String Parsing

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

String Parsing

Reply to Message Icon

Name: Madhulika
Date: August 3, 2009 at 04:25:22 Pacific
OS: Windows XP
Product: Microsoft Microsoft xp professional
Subcategory: Batch
Comment:

Hi ,

I have a string as WindowsXP-KB959542-Today.exe which need to be parsed to get only KB959542 . Can any one please help me in parsing this string in a batch file..Its Urgent plz



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: August 3, 2009 at 04:41:42 Pacific
Reply:

for /f "tokens=2 delims=-" %%a in ("WindowsXP-KB959542-Today.exe") do echo %%a


0

Response Number 2
Name: Madhulika
Date: August 3, 2009 at 04:47:58 Pacific
Reply:

Thanks a bunch...

Can you suggest me what i need to change in the script.. if such strings( WindowsXP-KB959542-Today.exe) are there in a .txt file in each line...

Thanks...


0

Response Number 3
Name: ghostdog
Date: August 3, 2009 at 05:47:08 Pacific
Reply:

vbscript

Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
	strLine = objFile.ReadLine
	s = Split(strLine,"-")
	WScript.Echo s(1)
Loop
objFile.Close

save as myscript.vbs and on command line

c:\test> cscript /nologo myscript.vbs

GNU win32 packages | Gawk


0

Response Number 4
Name: Judago
Date: August 3, 2009 at 05:53:55 Pacific
Reply:

For the batch version:

for /f "usebackq tokens=2 delims=-" %%a in ("your text file") do echo %%a


0

Response Number 5
Name: Madhulika
Date: August 3, 2009 at 22:43:05 Pacific
Reply:

Thanks!!

Can you please also help me in storing the for loop value each time in a variable ..


0

Related Posts

See More



Response Number 6
Name: Judago
Date: August 4, 2009 at 02:47:02 Pacific
Reply:

"Can you please also help me in storing the for loop value each time in a variable .."

Perhaps this is what your after:

setlocal enabledelayedexpansion
for /f "usebackq tokens=2 delims=-" %%a in ("your text file") do (
    set /a cnt+=1
    set var!cnt!=%%a
)


0

Response Number 7
Name: Madhulika
Date: August 10, 2009 at 03:50:59 Pacific
Reply:

Thanks for your timely help!!

I need one more suggestion in following problem..plz help me out!!

There are two text files with five lines each. I want to read both text files and have first line from one file and first line from second test file ..do some operation...then again second line from one file and second line form second file. and do operattion...so on uptill 5 lines...


Please help me in this....as i am new to batch file creation...

Thanks in advance..


0

Response Number 8
Name: Madhulika
Date: August 10, 2009 at 23:53:23 Pacific
Reply:

Please give me answer for this....


0

Response Number 9
Name: Judago
Date: August 11, 2009 at 00:43:03 Pacific
Reply:

Reading multiple files concurrently is problematic in batch files, it is possible but takes a lot of mucking around. Blank lines can also be a problem..

Here's a quick job, it's quite inefficient(and a bit of an abuse of find and findstr..) but seems to work......

for /f %%a in ('type "file1"^|find /c /v ""') do set /a size=%%a
for /l %%a in (1,1, %size%) do (
	for /f "tokens=2 delims=-" %%b in ('type "file1"^|find /n /v "" ^|findstr /b /l "[%%a]"') do (
		for /f "tokens=2 delims=-" %%c in ('type "file2"^|find /n /v "" ^|findstr /b /l "[%%a]"') do (
			echo %%b --- %%c
		)
	)
)

This code Assumes:
1. That both files have the same number of lines.
2. Both files contain the same type of data.
3. That there is no blank lines.


On a side note, you should show a little more initiative than just ask for someone to code it for you, the net is jam pack with examples and reference material.....


0

Response Number 10
Name: Madhulika
Date: August 11, 2009 at 02:33:59 Pacific
Reply:

Thanks for suggestion....But I am really new to the batch scripting....

Anyways..I have checked for my data..it is not working as it has data in each line and two text files are seperate

Atlast help me with one thing...can we delete a line in a text file thru for loop..i searched..but no where i found the help...

Hoping you will help me...Judago!!


0

Response Number 11
Name: Judago
Date: August 11, 2009 at 05:50:17 Pacific
Reply:

Anyways..I have checked for my data..it is not working as it has data in each line and two text files are seperate

Atlast help me with one thing...can we delete a line in a text file thru for loop..i searched..but no where i found the help...

The Code I posted in #9 does run over two files("file1" and "file2"), albeit inefficiently and in very limited scope.

To delete a line you have to run a for loop over the file, exclude the unwanted line, output to a new file the delete the old file and rename the new file or move the new file over the top of it.

This should work to exclude a line by line number(blank lines will cause an issue).

set cnt=
set exclude=8
for /f "usebackq delims=" %%a in ("your file") do (
    set /a line+=1
    for /f %%b in ('call echo %%line%%') do (
        if not %%b==%exclude% (
            >> newfile echo %%a
        )
    )
)
move "%cd%\newfile" "%cd%\yourfile"


0

Response Number 12
Name: ghostdog
Date: August 11, 2009 at 19:05:32 Pacific
Reply:

the alternative way to exclude a line is using find or findstr with /v option.

GNU win32 packages | Gawk


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: String Parsing

Batch Scripts - string parsing www.computing.net/answers/programming/batch-scripts-string-parsing/12733.html

Javascript String Parsing www.computing.net/answers/programming/javascript-string-parsing/11424.html

batch file string parsing www.computing.net/answers/programming/batch-file-string-parsing/16603.html