In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you the "batch FOR Delims and Tokens how to use", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "batch FOR in Delims and Tokens how to use" this article.
In the parameter F of the For command statement, the two options Delims and Tokens are the most difficult to understand. This paper simply makes a comparison and summary.
"For / f" is often used to parse text and read strings. In terms of division of labor, delims is responsible for segmenting strings, while tokens is responsible for extracting strings. If the string is used as a cake, Delims is like a knife to cut the cake, and tokens is like a fork to take the cut cake. Let's use examples to understand.
Save the following as a text file "song list .txt" with the extension ".txt":
Serial number, singer's name-song name. Suffix name
1. Rao Tianliang-rose lover. Wma
2. High one-I don't want to miss .mp3
3. A person with a sense of bitterness. MP3
4. Yellow can-Yellow Rose. LRC
5. Li Zi-such a love story. Mp3
Code 1: show all content
@ echo off
For / f% I in (song list .txt) do echo% I
Pause > nul
Running result:
Serial number, singer's name-song name. Suffix name
1. Rao Tianliang-rose lover. Wma
2. High one-I don't want to miss .mp3
3. A person with a sense of bitterness. MP3
4. Yellow can-Yellow Rose. LRC
5. Li Zi-such a love story. Mp3
Explanation:
If you do not use the parameter "/ f", the run result only displays the text character "song list .txt" in parentheses, but cannot read the contents of the text file "song list .txt". It can be seen that "/ f" is a good tool for parsing text strings.
1. Delims
If only the serial number, no singer name, song name and suffix name, how to do it?
Code 2: extract the first column by default
@ echo off
For / f "delims=," I in (song list .txt) do echo% I
Pause > nul
Running result:
Serial number
one
two
three
four
five
Explanation:
"delims=," means to define a tick, "is the delimiter, and splits the text string with the delimiter". Strings are the contents of the "song list. Txt", that is, the text and punctuation marks in the file.
The sign is in the original text. In addition to the ",", there are minus signs-"and periods" in the original text, so you can also use them as delimiters.
Code 3: use the minus sign "-" as the delimiter
@ echo off
For / f "delims=-"% I in (song list .txt) do echo% I
Pause > nul
Running result:
Serial number, singer's name
1. Rao Tianliang
2. A high song
3. Parsley
4. Huang can
5. Li Zi
Explanation:
Because, when the minus sign "-" is used as the delimiter, each line is separated by the minus sign into two halves, and only the first half is displayed by default, while the second half together with the delimiter minus sign is ignored (omitted).
Code 4: use a period. Act as a delimiter
@ echo off
For / f "delims=."% I in (song list .txt) do echo% I
Pause > nul
Running result:
Serial number, singer name-song name
1. Rao Tianliang-Rose lover
2. High one-I don't want to miss it.
3. A person with a sense of arrogance.
4. Yellow can-Yellow Rose
5. Li Zi-such a love story
Explanation:
By default, when using delims instead of tokens, only the content before the first delimiter is displayed, and the content after the first delimiter and the first delimiter is ignored.
Code 5: define multiple delimiters
@ echo off
For / f "delims=, -."% I in (song list .txt) do echo% I
Pause > nul
Running result:
Serial number
one
two
three
four
five
Explanation:
The reason is that when defining don signs, minus signs, minus signs, and periods. After the three punctuation marks are delimited, the original text is divided into four parts.
For example, the second line "1, Rao Tianliang-Rose lover .wma" will be divided into "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 text has four columns: "serial number", "singer's name", "song name" and "suffix name".
Typically, only the contents of the first column are read. The rest needs to be extracted with the tokens option.
The above is all the content of the article "how to use Delims and Tokens in batch FOR". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.