Through the above learning, we found that for/f will use each line as an element by default, but what should we do if we still want to decompose each line into smaller content? Don't worry, the for command also provides us with more detailed parameters, making it possible for us to split each line into smaller elements.
They are: delims and tokens
Delims is used to tell for each line what should be used as a delimiter. The default delimiters are spaces and tab keys
For example, if it is the above file, we execute the following command:
for /f "delims= " %%i in () do echo %%i
The results displayed are:
Row 1, column 1
Line 2, column 1
Line 3 Column 1
Why is this? Because there is the parameter delims here, there is a space after =, which means that each element is divided by spaces. By default, only the first element after the segmentation is taken.
The execution process is:
Divide the first element "Line, Column 1, Column 1, Column 2, Column 1, Column 3" into three elements: "Line, Column 1, Column 1, Column 2, Column 1, Column 3", it only takes the first one by default, that is, "Line, Column 1, Column 1", and then execute the command after do, and so on.
But this is still limited. If we want the second column element of each row, so what?
At this time, tokens jumped out and said, I can do it.
Its purpose is to control which one or more to take when you divide each row into smaller elements through delims.
As for the above example, execute the following command:
for /f "tokens=2 delims= " %%i in () do echo %%i
Execution results:
Row 1, column 2
Row 2, column 2
Line 3, column 2
If you want to display the third column, change to tokens=3.
At the same time, tokens supports wildcard characters* and limited scopes.
If you want to display the second and third columns, change to tokens=2, 3 or tokens=2-3, and if there are more, it is: tokens=2-10 and so on.
The command at this time is:
for /f "tokens=2,3 delims= " %%i in () do echo %%i %%j
How to get an extra %%j?
This is because after your tokens, you need to take two columns of each row, replace the second column with %%i, and replace the third column with %%j.
And it must be arranged alphabetically in English. %%j cannot be replaced with %%k, because i is followed by j
The execution result is:
Line 1, column 2, row 1, column 3
Line 2 Column 2 Column 2 Column 3
Line 3 Column 2 Line 3 Column 3
For wildcard characters*, it is to regard all or the rest of this line as an element.
for example:
for /f "tokens=* delims= " %%i in () do echo %%i
The execution result is:
Line 1 Column 1 Column 1 Column 2 Column 1 Column 3
2nd row, column 1, row, column 2, column 2, column 3
Line 3 Column 1, Line 3 Column 2, Line 3 Column 3
In fact, it is the same as the execution result of for /f %%i in () do echo %%i.
Another example:
for /f "tokens=2,* delims= " %%i in () do echo %%i %%j
The execution result is:
Line 1, column 2, row 1, column 3
Line 2 Column 2 Column 2 Column 3
Line 3 Column 2 Line 3 Column 3
Replace the second column with %%i and replace all remaining
Finally, there is skip and eol. These two simple things are. skip means ignoring the first number of lines of the file, and eol is used to specify which symbol a line starts with, ignore it.
for example:
for /f "skip=2 tokens=*" %%i in () do echo %%i
The result is:
Line 3 Column 1, Line 3 Column 2, Line 3 Column 3
Use skip to tell for to skip the first two lines.
If tokens=* is not added, the execution result is:
Line 3 Column 1
Don't know what's going on.
For example, when the content becomes:
.Line 1, Column 1, Column 2, Column 1, Column 3
.Line 2, column 1, row 2, column 2, column 3, row 2
Line 3 Column 1, Line 3 Column 2, Line 3 Column 3
Execution for /f "eol=. tokens=*" %%i in () do echo %%i result is:
Line 3 Column 1, Line 3 Column 2, Line 3 Column 3
Use eol to tell for to ignore lines starting with "."
Also, you must add tokens=*, otherwise only "Line 3, column 1" will be displayed.