Delims and tokens summary for in batch process _dos/bat
Source: Internet
Author: User
In the FOR command statement parameter F, the most difficult to understand is the delims and tokens two options, this article simple to make a comparison and summary.
"for/f" is often used to parse text and read strings. In division, DELIMS is responsible for splitting strings, while tokens is responsible for extracting strings. If the string is a cake, delims like a knife, used to cut the cake, tokens like a fork, to take the cut cake. Here we use examples to understand.
Save the following as a text file, "song list. txt", and note that the extension is ". txt":
Serial number, singer name-song name. Suffix name
1, Liatianliang-Rose lover. wma
2, one high-I don't want to miss it. mp3
3, the yellow celery-sad lovers. MP3
4, Huang Can-yellow rose. LRC
5, Li Chi-Such a love story. mp3
Code 1: Show All content
@echo off
For/f%%i in (song list. txt) do echo%%i
Pause>nul
Run Result:
Serial number, singer name-song name. Suffix name
1, Liatianliang-Rose lover. wma
2, one high-I don't want to miss it. mp3
3, the yellow celery-sad lovers. MP3
4, Huang Can-yellow rose. LRC
5, Li Chi-Such a love story. mp3
Explain:
If you do not use the parameter "/F", the run result displays only the text characters in parentheses, "song list. txt", and cannot read the contents of the text file "song list. txt". Visible, "/f" is a good tool for parsing text strings.
First, Delims
If as long as the serial number, not singer name, song name and suffix name, how to do?
Code 2: The first column is extracted by default
@echo off
For/f "delims=,"%%i in (song list. txt) do echo%%i
Pause>nul
Run Result:
Serial number
1
2
3
4
5
Explain:
"Delims=," indicates that comma "," is delimited, and splits the text string with the separator ",". The string is the content in the song list. txt, which is the text and punctuation in the file.
The comma is in the original. In addition to the comma "," there is also a minus sign "-" and point number "." So you can also use them as separators.
Code 3: Use the minus sign "-" to do the separator
@echo off
for/f "delims=-"%%i in (song list. txt) do echo%%i
Pause>nul
Run Result:
Serial number, singer name
1. Break Dawn
2, a high song
3, yellow celery
4, Huang can
5, Li Posture
Explain:
Because when the minus sign "-" is used as a separator, each line of content is separated by a minus sign "-", which only displays the first half, and the latter part, along with the delimiter minus sign "-", is ignored (omitted).
Code 4: With the dot number "." Make a separator
@echo off
For/f "delims=."%%i in (song list. txt) do echo%%i
Pause>nul
Run Result:
Serial number, singer name-song name
1, Liatianliang-Rose Lover
2, one high-I don't want to miss
3, the yellow celery-feelings of hurt lovers
4, Huang Can-yellow rose
5, Li Chi-The love Story
Explain:
By default, only the content before the first delimiter is displayed when you use DELIMS instead of tokens, and the content after the first and first delimiter is ignored.
Code 5: Define multiple delimiters
@echo off
for/f "delims= 、-."%%i in (song list. txt) do echo%%i
Pause>nul
Run Result:
Serial number
1
2
3
4
5
Explain:
The reason is that when you define comma ",", minus sign "-" and dot number. " After the three punctuation marks are delimited, the original text is divided into four parts.
As the second line of "1, Liatianliang-Rose lover. wma" will be divided into "1", "Break Dawn", "Rose Lover" and "WMA" four parts.
From the first line to the last line, each portion of each row corresponds to a vertical column. Therefore, the original text has "serial number", "singer Name", "Song name", "suffix name" four columns.
In general, only the contents of the first column are read. The following content needs to be extracted with the tokens option.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.