String Manipulation - Extract String of info after the colon

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
booga73
Posts: 108
Joined: 30 Nov 2011 16:16

String Manipulation - Extract String of info after the colon

#1 Post by booga73 » 06 Dec 2011 20:11

I have the following sample text file with this information, I was able to research and locate some already batch file code to extract string information and I modified it to my use. But, the corresponding output is a single string of modified, extracted string data separated by a comma, which is shown below. Where " tokens=2 " and " delims=: " (space between the colon and the " character; that's what I was able to modify. In this case though, I couldn't get the code to concisely be listed in my output file ( myoutput.txt ) a line at a time.

Instead of output of data:
code1,code2,code3an4,stringcodeNot3n4,fetchrandomanchor,code1,code2a,code3an4,stringcodeNot3n4,fetchrandomanchor01as,

I would like assistance to list the manipulated string of data 1 line at a time with no comma, please.

my cpulist.txt text file consists of the following string data that needs extracted:
--------------------------------
mother: code1
father01: code2
step1: code3an4
step02: stringcodeNot3n4
step03a4: fetchrandomanchor
01mother: code1
father0122: code2a
step123: code3an4
step0224: stringcodeNot3n4
step03a425: fetchrandomanchor01as
--------------------------------

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set csvar=
for /f "tokens=2 delims=: " %%a in (cpulist.txt) do set csvar=!csvar!%%a,
echo %csvar% > myoutput.txt
endlocal
rem pause


With assistance from a batch file **consored**, I would like the output of the data:

code1
code2
code3an4
stringcodeNot3n4
fetchrandomanchor
code1
code2a
code3an4
stringcodeNot3n4
fetchrandomanchor01as


thank you for your assistance, v/r Booga73

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: String Manipulation - Extract String of info after the c

#2 Post by Ed Dyreen » 06 Dec 2011 20:33

'
Tadaaa :mrgreen:

Code: Select all

@echo off &setlocal enableDelayedExpansion

for /f "usebackq eol= tokens=2 delims=: " %%? in (

   "cpulist.txt"

) do    echo.?=%%?_

pause
exit /b

Code: Select all

?=code1_
?=code2_
?=code3an4_
?=stringcodeNot3n4_
?=fetchrandomanchor_
?=code1_
?=code2a_
?=code3an4_
?=stringcodeNot3n4_
?=fetchrandomanchor01as_
Druk op een toets om door te gaan. . .

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: String Manipulation - Extract String of info after the c

#3 Post by booga73 » 06 Dec 2011 20:53

Ed Dyreen,

lol, awesome, thank you for your input. You presented a catch 29 for me to figure out; awesome. I did! :)

instead of this: echo.?=%%?_

I modified the code to this: echo.%%?

code1
code2
code3an4
stringcodeNot3n4
fetchrandomanchor
code1
code2a
code3an4
stringcodeNot3n4
fetchrandomanchor01as
Press any key to continue . . .

thank you! Happy Holidays to you Sir!

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: String Manipulation - Extract String of info after the c

#4 Post by booga73 » 07 Dec 2011 09:50

Ed Dyreen,

Is there a way to take the data that is being " stripped " an instead of placing the data into a text file create a batch array to temporary hold data till later processing?

best regards, Booga73

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: String Manipulation - Extract String of info after the c

#5 Post by Ed Dyreen » 07 Dec 2011 13:29

'
euh, like this :?:

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$cpulist=0" &for /f "usebackq eol= tokens=1,2 delims=-: " %%a in (

   "cpulist.txt"

) do (
   set /a $cpulist += 1
   set "$cpulist.!$cpulist!.vr=%%~a"
   set "$cpulist.!$cpulist!.vl=%%~b"
)

echo.$cpulist=!$cpulist!_

for /l %%? in (

   1, 1, !$cpulist!

) do (
   echo.$cpulist.indexOf[%%~?].vr=!$cpulist.%%~?.vr!_
   echo.$cpulist.indexOf[%%~?].vl=!$cpulist.%%~?.vl!_
)

pause
exit /b

Code: Select all

$cpulist=10_
$cpulist.indexOf[1].vr=mother_
$cpulist.indexOf[1].vl=code1_
$cpulist.indexOf[2].vr=father01_
$cpulist.indexOf[2].vl=code2_
$cpulist.indexOf[3].vr=step1_
$cpulist.indexOf[3].vl=code3an4_
$cpulist.indexOf[4].vr=step02_
$cpulist.indexOf[4].vl=stringcodeNot3n4_
$cpulist.indexOf[5].vr=step03a4_
$cpulist.indexOf[5].vl=fetchrandomanchor_
$cpulist.indexOf[6].vr=01mother_
$cpulist.indexOf[6].vl=code1_
$cpulist.indexOf[7].vr=father0122_
$cpulist.indexOf[7].vl=code2a_
$cpulist.indexOf[8].vr=step123_
$cpulist.indexOf[8].vl=code3an4_
$cpulist.indexOf[9].vr=step0224_
$cpulist.indexOf[9].vl=stringcodeNot3n4_
$cpulist.indexOf[10].vr=step03a425_
$cpulist.indexOf[10].vl=fetchrandomanchor01as_
Druk op een toets om door te gaan. . .
You'll have to be careful with quotes and delimiters that may appear in files. :idea:

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: String Manipulation - Extract String of info after the c

#6 Post by booga73 » 07 Dec 2011 22:40

Thank you very much.

I ran some scenarios to test out that initial code you wrote. . .

initial code (modified):
-------------------------------
@echo off &setlocal enableDelayedExpansion
for /f "usebackq eol= tokens=2 delims=: " %%? in (
"c:\temp\sysinfo.txt"
) do echo.%%? > textFile.txt
pause
exit /b
-------------------------------

for instance,

systeminfo | findstr /c:"Host Name:" >> c:\temp\sysinfo.txt
systeminfo | findstr /c:"OS Name:" >> c:\temp\sysinfo.txt
systeminfo | findstr /c:"System Type:" >> c:\temp\sysinfo.txt
systeminfo | findstr /c:"OS Version:" >> c:\temp\sysinfo.txt

produced this in a text file, textFile.txt:

Name
Name
Type
Version
Press any key to continue . . .

what was in my text, sysinfo.txt, was the following data. From the colon, there isn't a set space between the colon and the corresponding data information and that there were more than 2 tokens. Forexample there could be like 21 spaces to possibly 14 spaces.

This is the actual information made from systeminfo findstr command from above directed into sysinfo.txt.
Host Name: OWNER-PC
OS Name: Microsoft Windows 7 Professional
System Type: x64-based PC
OS Version: 6.1.7601 Service Pack 1 Build 7601

I was thinking that due to the space gab between the colon and data, the code got confused since tokens=2 delims=: " only specified two tokens and a space.

your code worked fine for cpulist.txt from above.

v/r Booga73

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: String Manipulation - Extract String of info after the c

#7 Post by booga73 » 08 Dec 2011 17:11

OS Version: 6.1.7601 Service Pack 1 Build 7601
BIOS Version: Dell Inc. A13, 7/28/2008


Problem with batch code still. I appologize with my prior post if it didn't sound right, it was late at night and was tired.

systeminfo | findstr /c:"OS Version:" > c:\temp1\irtmp\OSVersion.txt, which produces this text file, osversion.txt.
-----------------------
OS Version: 6.1.7601 Service Pack 1 Build 7601
BIOS Version: Dell Inc. A13, 7/28/2008
-----------------------

The code above which was provided assistance with will produce output, but when I try to modify then adapt the code to split the string data, the output of the information is broken.

For instance, when I run the following:
------------------------------

setlocal enableDelayedExpansion
for /f "usebackq eol= tokens=3 delims=: " %%? in (
"C:\temp1\OSVersion.txt"
) do echo.%%? > c:\temp1\systmp.txt

pause
------------------------------

the output in systmp.txt is simply " Dell " and not the following:
-----------------------
6.1.7601 Service Pack 1 Build 7601
Dell Inc. A13, 7/28/2008
-----------------------

Why is that? Can you still assist with the correct string output Sir?

best regards, Booga73

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: String Manipulation - Extract String of info after the c

#8 Post by Ed Dyreen » 08 Dec 2011 17:29

'
Is this what you're looking for :?:
"osversion.txt"

Code: Select all

-----------------------
OS Version: 6.1.7601 Service Pack 1 Build 7601
BIOS Version: Dell Inc. A13, 7/28/2008
-----------------------

Code: Select all

@echo off &setlocal enableDelayedExpansion

for /f "usebackq tokens=1,* delims=:-" %%a in (

   "osversion.txt"

) do (
   set "$ID=%%~a"
   set "$vl=%%~b" &set "$vl=!$vl:* =!"
   echo.$ID=!$ID!_
   echo.$vl=!$vl!_
)

pause
exit /b

Code: Select all

$ID=OS Version_
$vl=6.1.7601 Service Pack 1 Build 7601_
$ID=BIOS Version_
$vl=Dell Inc. A13, 7/28/2008_
Druk op een toets om door te gaan. . .
Notice how I use the tokens to strip the '------' lines, for is probably the most powerful command in DOS. :D
ps; Don't call me sir Sir, I'm only 10 :lol:

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: String Manipulation - Extract String of info after the c

#9 Post by booga73 » 08 Dec 2011 22:07

Hi Ed,

I ran the code you provided, modified a little . .. .

modified the code alittle to the following:
---------------------------
@echo off &setlocal enableDelayedExpansion

for /f "usebackq tokens=1,* delims=:-" %%a in ( "osversion.txt" ) do (
rem set "$ID=%%~a"
set "$vl=%%~b" &set "$vl=!$vl:* =!"
rem echo.$ID=!$ID!_
rem echo.$vl=!$vl!_
echo.!$vl!
echo.!$vl! >> c:\temp1\newtemp.txt
)

pause
exit /b
---------------------------

the command, echo.!$vl! produces this in the newtemp.txt data file and on screen in the dos window:

6.1.7601 Service Pack 1 Build 7601
Dell Inc. A13, 7/28/2008
Press any key to continue . . .


there is leading space before the string output, is there a way to prevent that?

v/r booga73

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: String Manipulation - Extract String of info after the c

#10 Post by Ed Dyreen » 08 Dec 2011 22:37

'
Redirection outside 'for' loop is thought to be cleaner, safer and faster. :idea:

Code: Select all

@echo off &setlocal enableDelayedExpansion

>> c:\temp1\newtemp.txt (
   for /f "usebackq tokens=1,* delims=:-" %%a in ( "osversion.txt" ) do (
      rem set "$ID=%%~a"
      set "$vl=%%~b" &set "$vl=!$vl:* =!"
      rem echo.$ID=!$ID!_
      rem echo.$vl=!$vl!_
      echo.!$vl!
   )
)

pause
exit

Code: Select all

6.1.7601 Service Pack 1 Build 7601
Dell Inc. A13, 7/28/2008
Druk op een toets om door te gaan. . .

Post Reply