Batch for parameter/F tokens detailed _dos/bat

Source: Internet
Author: User
(iv) TOKENS=X,Y,M-N extraction column


Format:

for/f "Tokens=x,y,m-n"%%i In (COMMAND1) do Command2

Usage:

Summary of the sentence: Extract the column.

Popular speaking, together to extract the contents of the section m of each line.

Therefore, you can use this command to specify the extraction of textual information.

Tokens= sometimes represents the extraction of all.

Tokens=m represents the extraction of column M.

Tokens=m,n represents the extraction of columns M and nth.

Tokens=m-n represents the extraction of column m to nth columns.

Tokens=* indicates that the space preceding each line is deleted. Ignores all spaces at the beginning of the line.

tokens=m* extracts all characters after the M column, and the asterisk indicates the remaining characters.

tokens=m,* extracts all characters after the M column, and the asterisk indicates the remaining characters.

The number of output variables is determined by the tokens defined.

Explicitly declares the%%i in the For statement. Use the tokens= implicit declaration of%%j and%%k. You can specify up to 26 output variables by using tokens= as long as it does not cause an attempt to declare a variable above the letter "z" or "Z."


Then the previous example "Quiet Night thinking".

If I want to extract the third bar "Look up to the moon," How to do?

@echo off

For/f "delims=, tokens=3"%%i in (Silent Night) do echo%%i

Pause>nul


Explain:

First with delims=, the command should use commas as the delimiter of the verse to divide the four sentences into four bars. 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, and the for command returns only one of them if the double quotes are used separately. Because in the first section, the for is read the command one at a time, after separating delims and tokens, for a single read, not all at once.


Attention:

Tokens are often used in conjunction with DELIMS.

First, a line of content is delims separated into many small sections or bars by dividing symbols such as commas.

The tokens can then extract each of these small segments or bars that correspond to each row-the column. See the example below.


Example: Extract the previous example text "A.txt" in the third paragraph of each line "CCC", "KKK", "GGG", "Examination."

@echo off

for/f "Skip=1 delims=, tokens=3"%%i in (a.txt) do echo%%i

Pause>nul

Note: Why does KKK not appear?


Example: how to block the punctuation in the text "A.txt"?

@echo off

For/f "skip=1delims=, Tokens=1-4"%%i in (a.txt) do echo%%i%%j%%k

Pause>nul


Explain:

%%i%%j%%k%%l is an output variable whose number is determined by the tokens (m-n), usually (n-m+1), but not more than Z.

And the letter after the% percent of the order,%%a%%b is followed, the reverse is reversed.

The tokens=m* of the line number, and the asterisk indicates all remaining columns after M.

Cases:

for/f "tokens=*"%%i in (a.txt) do echo%%i

Explain:

Without specifying a specific column, "tokens=*" Extracts all the columns, followed by an output variable%%i.


Cases:

for/f "tokens=2*"%%i in (a.txt) do echo%%i%%j

Explain:

"Tokens=2*" extracts all the characters after the second column, and the asterisk indicates the remaining characters. The output variable%%i corresponds to an input variable of 2,%%j the input variable corresponding to the asterisk.

Attention:

Delisms is not used here but is separated by a space because the for default space is delimited.


Example: Shielding the punctuation marks in the "I Ching:"

@echo off

for/f "Tokens=1-5 delims=:,"%%a in (I Ching) do echo%%a%%b%%c%%d%%e

Pause>nul

Explain:

When Delims defines two delimiters ":" and "," the extraction column uses tokens.

There are five columns, so tokens to define the extraction of the first column to the fifth column, that is, 1-5, and, accordingly, the subsequent need for five output display variables%%a%%b%%c%%d%%e. You can also modify this to avoid too many variable symbols "supplements"? 】

You can also define the last excess period as the delimiter "delims=:,." "Block it off.


Practice:

1, please speak briefly about the role of tokens

2, tokens=1,3-5 after the output variable how many?

3, please use tokens to extract the title of the following Web page "hard disk knowledge and Failure Grand Center (page 2)":

Mobile phone, 3G mobile phone, smartphone, mobile movies, mobile phone software, mobile games, ringtones, themes, pictures, e-books, cell phone repair, mobile phone encyclopedia, mobile quotes, downloads, smartphone testing, use of knowledge and hard disk information (page 2)-Computer technology Exchange Zone-3g365 smartphone forum- Powered by discuz! Archiver.mht

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.