Can I have 2-3 letters as Delimeter?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
arkamath7
Posts: 3
Joined: 26 Oct 2009 00:28

Can I have 2-3 letters as Delimeter?

#1 Post by arkamath7 » 26 Oct 2009 00:52

Hi,
I have a Text file where in the Delimeter should be TWO continuous digits(Double quote followed be a White Space). Can I have this in the below for stmt? The below stmt doesn't work

For /F "tokens=2* delims=" " %%i in (c:\File.txt) do echo %%i

The below line perfectly works if the delimiter is only a White space. I can't have delimeter as White space in my code, because it doesn't work

For /F "tokens=2* delims= " %%i in (c:\File.txt) do echo %%i

Can somebody correct the above FOR statement?

Regards
Aravind

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 26 Oct 2009 08:58

First, you cannot have multiple character delimiters.

Second, you cannot have a double quote as a delimiter.

You would have to grab the entire line as a variable, and then work your way manually through the variable one character at a time to find your double character delimiters. Something like this:

setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%a in (c:\file.txt) do call :process "%%~a"
goto :eof

:process
set "tmpvar=%~1"
set a=0
set b=1
set token=0
:loop
call set char2=%%tmpvar:~%b%,1%%
if not defined char2 echo Worked through the entire variable&&goto :finish
call set char1=%%tmpvar:~%a%,1%%
if ^%char1%6%char2%==^"^ echo We found a delimiter.&&set /a a+=2&&set /a b+=2&&set /a token+=1&&goto :loop
set token%token%=!token%token%!%char1%
set /a a+=1
set /a b+=1
goto :loop

:finish
for /l %%a in (0,1,%token%) echo token%%a=!token%%a!


Note this is completely untested.

arkamath7
Posts: 3
Joined: 26 Oct 2009 00:28

#3 Post by arkamath7 » 27 Oct 2009 21:36

Hi,
1st of all my sincere thanks to you for replying to my query. I think I have almost resolved this issue. Because of some time crunch I am not able to dump the code...

Once I complete I will post How is that i could do it.

Thanks once again.

Regards
Aravind

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#4 Post by jeb » 31 Oct 2009 09:24

Hi arkamath,

a general solution for splitting a string at the first delim, working with more than one char.


Code: Select all

call :split "abcdefXYZghijXYZklmnop" "XYZ" PrefixVar PostfixVar
echo '%PrefixVar%' '%PostfixVar%'
goto :eof

::::::::::::::
:split <string> <delim> <prefixResultVar> <postfixResultVar>
:::: Splits the <string> at the first <delim> into <prefix> and <postfix>
rem setlocal enabledelayedexpansion
setlocal
set "var=%~1"
call set "split_prefix=%%var:%~2=&::%%"
call set "split_postfix=%%var:*%~2=%%"
(
   endlocal
   set %~3=%split_prefix%
   set %~4=%split_postfix%
   goto :eof
)


hope it helps
jeb

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#5 Post by avery_larry » 02 Nov 2009 11:08

Ahh -- much better idea. To extend that, you could do a substring search/replace on whatever multiple character delimiter you wanted and replace it with a single character, and then work with the new string however you'd like in a for loop -- using the character you replaced your actual delimiter with.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Can I have 2-3 letters as Delimeter?

#6 Post by ghostmachine4 » 03 Nov 2009 00:54

arkamath7 wrote:Hi,
I have a Text file where in the Delimeter should be TWO continuous digits(Double quote followed be a White Space). Can I have this in the below for stmt? The below stmt doesn't work

For /F "tokens=2* delims=" " %%i in (c:\File.txt) do echo %%i

The below line perfectly works if the delimiter is only a White space. I can't have delimeter as White space in my code, because it doesn't work

For /F "tokens=2* delims= " %%i in (c:\File.txt) do echo %%i

Can somebody correct the above FOR statement?

Regards
Aravind

why don't you use a better alternative that let's you do splitting with multiple characters instead of spending time crafting workarounds in batch

a vbscript alternative, delimiter is the 2 digits "12"

Code: Select all

Set objFS=CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfLine
   linenumber = objFile.Line
   strLine = objFile.ReadLine
    csv = Split(strLine,"12")
    For i=LBound(csv) To UBound(csv)
       WScript.Echo csv(i)
    Next   
Loop
objFile.Close


output

Code: Select all

C:\test>more file
this is a 12 line with 12 delimeter

C:\test>cscript /nologo test.vbs
this is a
 line with
 delimeter


or with gawk for windows:http://gnuwin32.sourceforge.net/packages/gawk.htm

Code: Select all

c:\test> gawk -F"12" "{for(i=1;i<=NF;i++)print $i}" file 
this is a
 line with
 delimeter

[/url]

isacmahad
Posts: 44
Joined: 29 Oct 2009 23:08

#7 Post by isacmahad » 03 Nov 2009 01:33

jeb wrote:Hi arkamath,

a general solution for splitting a string at the first delim, working with more than one char.


Code: Select all

call :split "abcdefXYZghijXYZklmnop" "XYZ" PrefixVar PostfixVar
echo '%PrefixVar%' '%PostfixVar%'
goto :eof

::::::::::::::
:split <string> <delim> <prefixResultVar> <postfixResultVar>
:::: Splits the <string> at the first <delim> into <prefix> and <postfix>
rem setlocal enabledelayedexpansion
setlocal
set "var=%~1"
call set "split_prefix=%%var:%~2=&::%%"
call set "split_postfix=%%var:*%~2=%%"
(
   endlocal
   set %~3=%split_prefix%
   set %~4=%split_postfix%
   goto :eof
)


hope it helps
jeb


MARVELOUS!
I like this method very much.

ghostmachine4 wrote:why don't you use a better alternative that let's you do splitting with multiple characters instead of spending time crafting workarounds in batch


Is gawk native NO!!!
And though vbscript may be better suited, why not just meet in the middle and go grab yourself powershell. At least then you will save yourself extra lines of unneeded vbscript.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#8 Post by ghostmachine4 » 03 Nov 2009 01:46

isacmahad wrote:Is gawk native NO!!!

who cares!! people like you have weird thinking. So if its not native, then it can't be used?? take a similar situation. diruse is in the resource kit, its not native, does it mean it can't be used if i want to find the total disk space taken up by certain directories easily without have to craft out mountains of code , taking each directory's size and adding them up one by one??

unless the OP has restrictions, my suggestion is way better than wasting time doing workarounds in batch. Please, we are living in the 21st century and computing/sysadmin is not like decades ago. Humans evolve, so does software and computers. There are tools/languages that makes life easier for a sysadmin/programmer now, so use them.

And though vbscript may be better suited, why not just meet in the middle and go grab yourself powershell. At least then you will save yourself extra lines of unneeded vbscript.

vbscript works from Win9X onwards. Try running powershell on winNT machines!! Now in that case, is powershell native NO!!!

Post Reply