for/f how to intercept any row in bat

Source: Internet
Author: User
Tags eol

I. Overview

For command switches There are many,/l,/f,/r. This is only for analysis of the for with/F, which is probably the most commonly used and strongest command, mainly used to process the output of files and some commands.

1. Command format:
(1). for/f ["Options"]%%i in (file) do command
(2). for/f ["Options"]%%i in ("string") do command--note double quotes
(3). for/f ["Options"]%%i in (' command ') do command--Note single quotes

2. Parameter Description:

The file represents one or more files, and you can use wildcard characters.
String to represent strings
command on behalf of commands
The [options] option has multiple:

Eol=c-Specifies a line comment character that is ignored when the line that starts with C is encountered.
Skip=n-Specifies the number of rows to ignore at the beginning of the file.
DELIMS=XXX-Specifies the delimiter character. The default is Space and tab.

 tokens=x,y,m-n  -refers to which symbol per line is passed to each iteration
                     for itself. This causes the allocation of additional variable names. M-n
                  The    format is a range. Specify MTH with the nth symbol. If
                  The last character in the    symbol string asterisk number,
                     then the extra variable will be parsed after the last symbol
                     assigns and accepts the retained text for the row.
 usebackq        - 1. Use single-quote strings as commands; 2. Double quotation marks are allowed to expand the file name.

Second, Detailed introduction
1.for/f%%i in (file) do command
File is the file name, according to the official, for will open files in the file, and before the next file to read each file into memory, according to each behavior of an element, ignoring blank lines.
If the file D:\out.txt has the following content:

1th row 1th column 1th row 2nd column 1th row 3rd column
2nd row 1th column 2nd row 2nd column 2nd row 3rd column
3rd row 1th column 3rd row 2nd column 3rd row 3rd column

How do I traverse the contents of a file? The following statements can:
for/f%%i in (d:\out.txt) do echo%%i


Execution procedure: For will open OUT.txt first, then read out all the contents of OUT.txt, as a collection, and in each row (the file is no delimiter) as an element, with%%i in turn instead of each element, and then execute the command after the do.


FOR/F will default to each row (no delimiter) as an element, but what about delimiters? If the OUT.txt content becomes as follows:

1th row 1th column 1th row 2nd column 1th row 3rd column
2nd row 1th column 2nd row 2nd column 2nd row 3rd column
3rd row 1th column 3rd row 2nd column 3rd row 3rd column

Then for/f%%i in (d:\out.txt) do echo%%i will not be able to display all the content. The results appear as follows:

1th Row 1th Column
2nd row 1th Column
3rd row 1th Column

Because the command by default is separated by a space and tab, it encounters a delimiter, and by default only takes the first one, discarding all the delimiters.

What if we want to break down the smaller content of each line? The for command provides additional parameters, which are: Delims and tokens
Delims: Tell for each line what to take as a delimiter, the default delimiter is a space and the TAB key
For example, we execute the following command:

for/f "delims="%%i in (d:\out.txt) do echo%%i

The results displayed:

1th Row 1th Column
2nd row 1th Column
3rd row 1th Column

This command works the same as for/f%%i in (d:\out.txt) do echo%%i.

What if we want the elements behind each line? This time you can take advantage of the tokens parameter, which is the function of when you divide each line into smaller elements through delims, tokens to control which or which to take.
Or the above example, execute the following command:

for/f "tokens=2 delims="%%i in (d:\out.txt) do echo%%i

Execution Result:

1th Row 2nd Column
2nd row 2nd Column
3rd row 2nd Column
If you want to display the second and third columns, replace with tokens=2,3 or tokens=2-3, and all display with a wildcard character tokens=*.


Note: If you display multiple elements (not *), such as tokens=2-3 here, the statement should be written like this:

for/f "tokens=2,3 delims="%%i in (d:\out.txt) do echo%%i%%j

How to make a%%j more?
This is because your tokens takes two columns of each row, replaces the second column with%%i, and replaces the third column with%%j.
and must be in alphabetical order,%%J can not be replaced with%%k, because I is behind J.
The result of the execution is:

1th row 2nd Column 1th row 3rd column
2nd row 2nd Column 2nd row 3rd column
3rd row 2nd Column 3rd row 3rd column


Again, as in the following statement:

for/f "tokens=2,* delims="%%i in (d:\out.txt) do echo%%i%%j
It shows the column from the second column to the last, with the following results:

1th row 2nd Column 1th row 3rd column
2nd row 2nd Column 2nd row 3rd column
3rd row 2nd Column 3rd row 3rd column

Replace the second column with%%i and replace all the remaining with%%j.


Finally there are skip,eol and USEBACKQ.

Skip is to ignore the number of lines before the file, EOL is used to specify when a line begins with what symbol, ignore it. Like what:

for/f "skip=2 tokens=*"%%i in (d:\out.txt) do echo%%i
The result is:

3rd row 1th column 3rd row 2nd column 3rd row 3rd column
Use skip to tell the for to skip the first two lines.

Again, when the OUT.txt content becomes:

#第1行第1列 1th row 2nd column 1th row 3rd column
#第2行第1列 2nd row 2nd Column 2nd row 3rd column
3rd row 1th column 3rd row 2nd column 3rd row 3rd column

for/f "eol=# tokens=*"%%i in (d:\out.txt) do echo%%i

The result is:

3rd row 1th column 3rd row 2nd column 3rd row 3rd column
Use EOL to tell the for to ignore the "." The line that begins.

USEBACKQ is the meaning of the reversal, he used the string ("OUT.txt") as a file, or the command (' command ') as a string.

For example:

for/f "Usebackq eol=# tokens=*"%%i in ("D:\out.txt") do echo%%i

The result is:

3rd row 1th column 3rd row 2nd column 3rd row 3rd column

If USEBACKQ is not added, "d:\out.txt" is treated as a string.

Another example:

for/f "Usebackq tokens=*"%%i in (' Echo-helloworld! ') do echo%%i

Results:

Echo HelloWorld, where the command is treated as a string.

If there is no USEBACKQ

for/f "tokens=*"%%i in (' echo helloworld! ') do echo%%i

Results:

helloworld!

2.for/f ["Options"]%%i in ("string") do command

Example:

for/f "tokens=2,* delims="%%i in ("It's an example!") Do Echo%%i%%j

Results:

is an example!

The processing of strings is similar to the processing of files, which is not mentioned here.

3.for/f ["Options"]%%i in (' Command ') do command

This command takes the result of command execution as a collection, followed by similar file processing, which is not mentioned here.


Method 1. Find the specified string with the Find command

123 @Echo OffFor /f"delims=" %%i in (‘Type 1.txt^|Find "疑似"‘do(Echo %%i)Pause
Ask:
If I do not know 1.txt content, only know a total of four lines, want to display the second line alone, how to write?
Chase Answer:
123456 @Echo Off&Setlocal EnabledelayedexpansionFor /f"delims=" %%i in (1.txt) do (Set /an+=1 If !n!==2 Echo %%i)Pause

Show only the second row

Ask:
I am more dull, did not read, but can achieve, thank you. If you can write a 1.txt in the 2 4 6 lines of the content assigned to the variable a B C will be added 20 points. Thank you
Chase Answer:
12345678 For /f"delims=" %%i in (1.txt) do (Set /an+=1 If !n!==2 Set a=%%iIf !n!==4 Set b=%%iIf !n!==6 Set c=%%i)Echo %a% %b% %c%Pause

for/f how to intercept any line in

Bat

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.