Can I have 2-3 letters as Delimeter?
Moderator: DosItHelp
Can I have 2-3 letters as Delimeter?
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
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
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
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:
Note this is completely untested.
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.
Hi arkamath,
a general solution for splitting a string at the first delim, working with more than one char.
hope it helps
jeb
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
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
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.
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Can I have 2-3 letters as Delimeter?
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
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.
-
- Posts: 319
- Joined: 12 May 2006 01:13
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!!!