In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces what is the use of tokens FOR parameters/F in batch processing. The text is very detailed and has certain reference value. Interested friends must read it!
tokens=x,y,m-n extract columns
Format:
FOR /F "tokens=x,y,m-n" %%I IN (Command1) DO Command2
Usage:
To sum it up in one sentence: extract columns.
In layman's terms, extract the contents of subsection m of each line together.
Therefore, this command can be used to specify extraction of text information.
tokens= sometimes indicates extraction of all.
tokens=m indicates extraction of the mth column.
tokens=m,n means extracting the mth and nth columns.
tokens=m-n indicates extraction of columns m to n.
Tokens=* indicates that the spaces before each line are removed. Ignore all spaces at the beginning of the line.
tokens=m* Extracts all characters from column m onwards, asterisk indicates the remaining characters.
tokens=m,* Extract all characters from column m onwards, asterisks represent the remaining characters.
The number of output variables is determined by the tokens defined.
Explicitly declare %%i in FOR statement. Implicitly declare %%j and %%k with tokens=. Use tokens= to specify up to 26 output variables, as long as it does not cause an attempt to declare a variable higher than the letter "z" or "Z."
Following the previous example of "Night Meditation."
If I wanted to extract the third verse,"look up at the moon," how would I do that?
@echo off
for /f "delims=, tokens=3" %%i in (quiet night thinking.txt) do echo %%i
pause>nul
Explanation:
First, use delims= to indicate that the command is to use commas as verse separators to divide four sentences into four sections. Then use tokens=3 to extract the third bar, which is "looking up at the moon."
delims= and tokens= share a pair of double quotes. If you use double quotes alone, the FOR command can only return one of them. Because as mentioned in Section 1, FOR reads commands one by one, separating delims from tokens allows FOR to read commands one at a time, not all at once.
Note:
Tokens are often used in conjunction with delims.
First, a line is divided into segments or sections by delims using dividers such as commas.
Tokens can then extract these segments or stanzas-columns between rows. See the following example.
Example: extract the third paragraph of each line in the text "a.txt" of the previous example,"ccc,""kkk,""ggg," and "exam."
@echo off
for /f "skip=1 delims=,, tokens=3" %%i in (a.txt) do echo %%i
pause>nul
Note: Why doesn't kkk appear?
Example: how to block punctuation marks in text "a.txt"?
@echo off
for /f "skip=1delims=,, tokens=1-4" %%i in (a.txt) do echo %%i %%j %%k %%l
pause>nul
Explanation:
%%i %%j %%k %%l is the output variable, the number of which is determined by (m-n) after tokens, usually (n-m+1), but cannot exceed Z.
And the letters after %% are sequential, %%a %%b is forward, and the reverse is reverse.
tokens=m*, asterisk indicates all remaining columns after m.
Example:
for /f "tokens=*" %%i in (a.txt) do echo %%i
Explanation:
No specific columns are specified,"tokens=*" will extract all columns followed by only one output variable %%i.
Example:
for /f "tokens=2*" %%i in (a.txt) do echo %%i %%j
Explanation:
"tokens=2*" extracts all characters after the second column, and asterisks indicate the remaining characters. The output variable %%i corresponds to the input variable of 2 and %%j corresponds to the input variable of asterisk.
Note:
Delisms are not used here but are separated by spaces, because FOR defaults to spaces as separators.
Example: Block out punctuation marks in "I Ching.txt":
@echo off
for /f "tokens=1-5 delims=:," %%a in (Yi Jing.txt) do echo %%a %%b %%c %%d %%e
pause>nul
Explanation:
When delims defines two delimiters ":" and "," the extracted column uses tokens.
There are five columns, so tokens are defined to extract the first column to the fifth column, i.e. 1-5, and accordingly, five output display variables are required after %%a %%b %%c %%d %e. You can also modify this to avoid excessive variable symbols.
You can also define the last extra period as the delimiter "delims=:,. "Block it out.
The above is "What is the use of tokens FOR parameters/F in batch processing". Thank you for reading all the contents of this article! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to 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.