Sunday, April 8, 2012

Scripting Batch File: Split String


Its been a very long time that I have posted something. I was creating certain batch files and came across the requirement of splitting strings and searching for specific strings. I thought of sharing the same to all, so that one can refer and get help in case one needs.

In order to split a string, one can use the FOR loop. The syntax of the command that one may use to split a string is as follows:

FOR /F "" %% IN ("") DO (statement/s)

Set of arguments to be used for splitting a string can be any combination of the below:
  • delims=xxx : specifies a delimiter set.  The default delimiters are space and tab (if not specified).
  • tokens=x,y,m-n : specifies which tokens from each line are to be passed to the for body for each iteration. This will cause additional variable names to be allocated.  The m-n form is a range, specifying the mth through the nth tokens.  If the last character in the tokens= string is an asterisk, then an additional variable is allocated and receives the remaining text on the line after the last token parsed.
  • usebackq : specifies that the new semantics are in force, where a back quoted string (``) is executed as a command and a single quoted string ('') is a literal string command and allows the use of double quotes ("") to quote file names in file-set.


Lets look into some examples now:

a) FOR /F %a in ("Testing this is a very simple task") do @echo %a

The above command would print Testing as output. That means, if you do not specify any argument, it would split the variable and set the value of variable %a as the first fragment.

b) FOR /F "tokens=1-7" %a in ("Testing this is a very simple task") do @echo %a %f %g

The above command would print "Testing simple task" as the output. When we specify the above command with tokens=1-7, it would split the string for each Space and assign it to a variable starting from %a. In detail, the command would set variables with values as follows:
    %a = Testing
    %b = this
    %c = is
    %d = a
    %e = very
    %f = simple
    %g = task

In the above command, one can also set numerals as variables. i.e., FOR /F "tokens=1-7" %1 in ("Testing this is a very simple task") do @echo %1 %6 %7 is also a valid command and would return the same output.

c) Now we can use the "delims=" argument to specify any different delimiters based on which the string should be split. For example, the following command splits the string based on delimiter "," (comma).

    FOR /F "tokens=1-7 delims=," %a in ("Testing,this,is,a,very,simple,task") do @echo %a %f %g

Output of the command would be same as that in example b.

d) Now, if you want to get a command executed and then split the output string, then use the command string within single back quotes ( '' ). For example, the command

FOR /F "tokens=1-2 delims=." %a in ('dir /b') do @echo %a would return all the file names without their extension.

    or else,

one can force the new semantics and then insert the "usebackq" argument and specify the command within back-quotes (``). Example: FOR /F "usebackq tokens=1-2 delims=." %a in (`dir /b`) do @echo %a

e) If you want to parse through a text file and then split strings with delimiters present in the lines, use the command without any quotes. Example: FOR /F "tokens=1-5 delims=," %a in (a.txt) do @echo %a
   
or else,

one can force the new semantics and then insert the "usebackq" argument and define the file name within double-quotes (""). Example: FOR /F "usebackq tokens=1-5 delims=," %a in ("a.txt") do @echo %a

f) If one uses the new semantics by inserting the "usebackq" argument, and wants to split a string, then the string should be provided between single quotes (''). Example: FOR /F "usebackq tokens=1-5 delims= " %a in ('dir /b') do @echo %a

No comments:

Post a Comment