In the for command statement parameter F, the most difficult to understand is the delims and tokens options. This article makes a simple comparison and summary.
"For/F" is often used to parse text and read strings. In terms of division of labor, delims is responsible for splitting strings, while tokens is responsible for extracting strings. If the string is used as a cake, delims is like a knife, used to cut the cake, tokens is like a fork, used to get the cut cake. The following example is used for understanding.
Save the content as the ultimate file. txt, And the extension name is. txt ":
No., artist name-song name. suffix
1、rao Tianliang-Rose lover .wma
2nd Percentile-I do not wish to go wrong
3. Qinqin-Zhuan. MP3
4. Yellow-yellow rose. LRC
5. Li zi-like this wonderful love story
Code 1: show all content
@ Echo off
For/F % I in (songs %.txt) Do echo % I
Pause> NUL
Running result:
No., artist name-song name. suffix
1、rao Tianliang-Rose lover .wma
2nd Percentile-I do not wish to go wrong
3. Qinqin-Zhuan. MP3
4. Yellow-yellow rose. LRC
5. Li zi-like this wonderful love story
Explanation:
If you do not use the "/f”" parameter, the running result only displays the text in the character number. txt, and you cannot read the contents in the audio file. txt. It can be seen that "/F" is a good tool for parsing text strings.
1. delims
How can I do this if I only need the serial number, no singer name, song name, or suffix?
Code 2: extract the first column by default
@ Echo off
For/F "delims =," % I in (songs %.txt) Do echo % I
Pause> NUL
Running result:
Serial number
1
2
3
4
5
Explanation:
"Delims =," indicates defining comma "," as the separator, and splitting text strings with the separator. The contents in a string are the text and punctuation marks in the file.
The pause number is in the original text. In addition to the comma (,), there are also minus signs (-) and dot (.) in the original text, so you can use them as separators.
Code 3: Use the minus sign "-" as the Separator
@ Echo off
For/F "delims =-" % I in (songs %.txt) Do echo % I
Pause> NUL
Running result:
No., artist name
1. Rao Tianliang
2. Senior One
3.
4. Huang can
5. Lizi
Explanation:
Because, when the minus sign "-" is used as the separator, each line of content is separated by the minus sign "-" into the first two halves. By default, only the first half is displayed, the last half is ignored (Omitted) together with the separator minus.
Code 4: Use the dot "." As the Separator
@ Echo off
For/F "delims =." % I in (songs %.txt) Do echo % I
Pause> NUL
Running result:
No., artist name-song name
1. Rao Tianliang-Rose lover
2. I don't want to miss the first one.
3. zookeeper
4. Yellow-yellow rose
5. Lizi-such a love story
Explanation:
By default, when delims is used instead of tokens, only the content before the first separator is displayed. The content after the first separator and the first separator is ignored.
Code 5: define multiple separators
@ Echo off
For/F "delims =,-." % I in ( .txt) Do echo % I
Pause> NUL
Running result:
Serial number
1
2
3
4
5
Explanation:
The reason is that when the three punctuation marks ",", "-", and "." are defined as delimiters, the original text is separated into four parts.
For example, the 1st segment of the second line will be divided into four parts: "1", "Rao Tianliang", "Rose lover", and "WMA.
From the first row to the last row, each part of each row corresponds to a vertical column. Therefore, the original article contains four columns: "Serial Number", "singer name", "Song name", and "suffix name.
Generally, only the content of the first column is read. The following content must be extracted using 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.