Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of findfindstr Command in batch processing DOS

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about the sample analysis of the findfindstr command in batch processing commonly used DOS. Many people may not know much about it. In order to let everyone know more, Xiaobian summarizes the following contents for everyone. I hope everyone can gain something according to this article.

I. String search: find

Search for strings in a file.

FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:][path]filename[ ...]]

/V Displays all lines that do not contain the specified string.

/C Displays only the number of rows containing strings.

/N Displays the line number.

/I Ignore case when searching for strings.

/OFF[LINE] Do not skip files with offline attribute sets.

"string" Specifies the string of text to search for,

[drive:][path]filename Specifies the file to search for.

Basic format: find "string to find" file to find (not in the current directory, you need to give the full path)

Example 1

find "abc" d:\abc.txt

Find the line for the string abc in abc.txt.

/I Ignore case when searching for strings.

Example 2

find /i "abc" d:\abc.txt

The parameter/i stands for "Ignore", which means ignore case. The/I parameter allows you to be case-insensitive about the string "abc" you are looking for.

/N Displays the line number.

Example 3

find /n "abc" d:\abc.txt

The parameter/n represents the English word "Number". With the/n argument we can find the line number of the string "abc".

/C Displays only the number of rows containing strings.

Example 4

find /c "abc" d:\abc.txt

Parameter/c is an abbreviation for the English word "Count." With the/c parameter we can count the number of rows containing the string "abc."

/V Displays all lines that do not contain the specified string.

Example 5

find /v "abc" d:\abc.txt

This parameter is used to find lines in the file that do not contain the specified string. This means finding lines that do not contain the "abc" string.

Note: The string you are looking for in the find command should be enclosed in double quotes.

II. String lookup enhancement: findstr

Look for strings in files.

FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/F:file]

[/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]

strings [[drive:][path]filename[ ...]]

/B Pair patterns at the beginning of a row.

/E Pair patterns at the end of a line.

/L Use search strings by word.

/R Use search strings as generic expressions.

/S Search in current directory and all subdirectories

Matching files.

/I Specifies that the search is case-insensitive.

/X Print exactly matching lines.

/V Print only lines that do not contain matches.

/N Prints the number of lines before each matching line.

/M Print only the file name if the file contains a match.

/O Prints the character offset before each matching line.

/P Ignore files with non-printable characters.

/OFF[LINE] Files with offline attribute sets are not skipped.

/A:attr Specifies color attributes with hexadecimal digits. See "color /? "

/F:file Reads a list of files from the specified file (/stands for console).

/C:string Use the specified string as a literal search string.

/G: fileGets the search string from the specified file. (/stands for console).

/D:dir Find a list of directories delimited by semicolons

strings The text to find.

[drive:][path]filename Specifies the file to find.

Separate search strings with spaces unless the argument has a/C prefix.

For example: 'FINDSTR "hello there" x.y' Look for "hello" in file x.y or

"there" 。'FINDSTR /C:"hello there" x.y' Look for "hello there" in file x.y.

1. Basic format: findstr " strings " [drive:][path]filename

Strings is what you want to find.

[rive:][path]filename Specifies the file to look for, path can default, default is the current directory.

Example 1

findstr "icq" 123.txt

Look in 123.txt for lines containing the three strings "icq."

/I Specifies that the search is case-insensitive.

Example 2

findstr /i "MSN" 123.txt

Look for lines in 123.txt that contain the three characters "MSN," case insensitive.

★/R Use search strings as regular expressions. The/R parameter emphasizes regular expression rules for interpreting strings. R - Right is positive. we

It is said that the right hand is the forehand, so it is extended to the right for positive and the left for negative.

Example 3

findstr /r "icq msn" 123.txt

Look for lines in 123.txt that contain "icq" or "msn," and separate multiple strings with spaces.

/S Search in the current directory and all subdirectories.

Example 4

findstr /s /i "MSN" *.txt

Search txt files in the current directory and all subdirectories for the string "MSN"(case insensitive).

/C:string Use the specified string as a literal search string.

Example 5

findstr /c:"icq msn" 123.txt

Look in 123.txt for lines containing the characters "icq msn." Note that here "icq msn" is a whole.

This parameter is mostly used to find strings that contain spaces.

★ When using findstr "I you he" test.txt, you can't find the content, but after adding the switch/i or/r, it is correct.

Maybe it's a bug in finding multiple pure Chinese strings; single pure Chinese strings don't have any problems.

2. Rules for using regular expressions in the findstr command

Quick reference for general expressions:

. wildcard: any character

* Repeat: zero or more occurrences of previous characters or categories

^Row position: Start of row

$Row position: End of row

[class] Character category: Any character in a character set

[^class] Complementary character category: Any character not in the character set

[x-y]Range: Any character within a specified range

\x Escape: literal usage of metacharacter x

\Word Position: End of Word

Wildcard and repetition rules, i.e. and *

Wildcard, that is, a period, represents any character, and can only be one, including letters, numbers, half-width symbols and spaces.

The repetition symbol, i.e. model *, represents the number of occurrences of the previous letter (the number of occurrences ranges from 0 to many times, and 0 means none).

findstr . 123. txt or findstr ". " 123.txt

Find any character in file 123.txt, excluding blank lines.

Example 6

findstr .* 2. txt or findstr ".* " 2.txt

Look for any character in file 123.txt, including blank lines.

Example 7

findstr ac* 123.txt

Look in file 123.txt for lines where an "a" string occurs and where c occurs zero or any number of times after a.

For example:

a

ac

acc

addc

And so on.

Example 8

findstr ak5* 123.txt

Look in file 123.txt for the occurrence of an "ak" string and any line where ak is followed by 0 or any 5. For example:

ak

ak5

akbbb

ak125

ak555

And so on.

● The rule of beginning and ending line characters, i.e.^and $

Example 9

findstr "^step" 123.txt

Look for lines in file 123.txt that begin with the step string.

For example:

stepkdka

step 456

Both lines match.

example 10

findstr "step$" 123.txt

Look in file 123.txt for lines ending with the step string.

For example:

123 dstep

123step

These two lines also match.

example 11

findstr "^step$" 123.txt

In file 123.txt, find the line with step at the beginning and step at the end, that is, step alone.

● Character set rules, i.e.[class]

① Indicates a match that contains any character in the set.

② Elements in this character set can be letters and numbers and general half-width characters, such as:}{ ,.] [etc., but double quotes "are not recognized. It can't be Chinese, Chinese

Not properly interpreted (Chinese characters are not ASCII).

If you insert wildcards and repeating symbols within a character set, that is,"[.*] "Will be... And * are considered ordinary characters, with no wildcards or repeated meanings.

example 12

findstr "[0-9]" 123.txt

Look for any line of numbers 0-9 in file 123.txt.

For example:

4kkb

1 lkka cc

Both lines match.

example 13

findstr "[a-zA-Z]" 123.txt

Look in file 123.txt for lines that include any letters.

example 14

findstr "[abcezy]" 2.txt

Look in file 123.txt for a line containing any of the letters a b c e z y.

example 15

findstr "[a-fl-z]" 2.txt

Look in file 123.txt for any line of lowercase characters a through f or l through z, but not the letters g h I j k.

example 16

findstr "M[abc][123]Y" 2.txt

Look in file 123.txt for lines matching Ma1Y , Mb1Y, Mc1Y; Ma2Y , Mb2Y, Mc2Y; Ma3Y , Mb3Y, Mc3Y.

Subtraction rules, i.e.[^class]

example 17

findstr "[^0-9]" 123.txt

Lines that are purely numeric are filtered out, such as strings like 2323423423, but not strings like 345hh888.

Note that pure numeric lines cannot have spaces, whether at the beginning or end of the line or in the line, otherwise the filter fails!

example 18

findstr "[^a-z]" 123.txt

If the line is pure letters, such as sdlfjlkjlksjdklfjlskdf will be filtered, if it is sdfksjdkf99999

The formula cannot be filtered.

Note that pure letter lines cannot have spaces, no matter the beginning and end of the line or the line cannot have spaces, otherwise the filtering fails!

example 19

findstr "[^add]" 123.txt

Filter lines that contain only pure alphabetic strings consisting of the letters a d d.

For example:

a

ad

ddaadd

dd

These lines are filtered.

Note that lines containing only pure letter strings consisting of a d d letters cannot have spaces, whether at the beginning or end of the line or in the line, otherwise

Filter failed!

example 20

findstr "[^echo]" 123.txt

Filter lines that contain only pure alphabetic strings of four letters e c h o.

For example:

e

c

ec

cho

chooo

These lines are filtered.

● word prefix suffix positioning rules, namely\

The xyz can be an English word or number, but does not apply to Chinese characters. The symbol\is understood as an escape character, resolving the meaning of the less than and greater than signs redirection commands.

This rule is matched to a single English word.

example 21

findstr "\" 123.txt

All lines containing strings ending with echo match.

For example:

The qq bbecho line also matches.

(Thinking: Why does kk echo: also match?)

example 23

findstr "\123.txt

This is used to find exact words. Find the line for the word end,

Note:

ended

cdkend

bcd-end-jjkk

None of these words match.

(Thinking: Why do end echo and end also match?) Because\format is looking for a single English word.)

● Escape character\

Convert special characters (metacharacters) in an expression to ordinary characters. Common Writing:

\.

\*

\\

\[

\]??

\-

example 24

findstr "\.abc" 123.txt

Look in file 123.txt for lines that match the string ".abc," where\. Yes. It's escaped.

example 25

findstr "1\\" 123.txt or findstr "1\\\" 123.txt

Look for lines in file 123.txt that match the string "1\," where\\is escaped.

★ When the string you are looking for contains\, you can escape\with\\; or change\to\\\. If there is more content after\in the target string, search for characters

In addition to becoming\\(its own escape requirement), the string\can be followed by another character, such as\\.

After reading the above, do you have any further understanding of the sample analysis of the findfindstr command in batch processing common DOS? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report