close

參考MS official web site:Findstr.....
Regular Expressions (Searching for patterns of text)

Character

Value

.

Wildcard: any character
搜尋任意字串(不含空字串或空白行)

*

Repeat: zero or more occurrences of previous character or class

^

Line position: beginning of line

找出每行開頭,符合指定的字串

$

Line position: end of line

找出每行結尾,符合指定的字串

[class]

Character class: any one character in set

找出符合[]內之字元資料。

[^class]

Inverse class: any one character not in set

相反於[class]的結果,就是不在集合內的內容才符合。

[X-y]

Range: any characters within the specified range

找出符合[]內之起迄字元資料 (符合之一即是)。[0-9][A-Za-z]。

\X

Escape: literal use of metacharacter X

這個功能尚未明瞭….。

\<xyz

Word position: beginning of word

找符合英文字開頭,例: \<wor 可找到hello world

xyz\>

Word position: end of word

找符合英文字結尾,例: \<lo 可找到hello  world

 

範例: 

1.findstr   .   c:\f.txt
   從文件c:\f.txt中,顯示非空白之任意字元。

2.*號
   2.1 ".*",有""包含著,搜索的條件是任意字串,

   findstr   ".*"   c:\f.txt
     從文件c:\f.txt中,搜尋所有內容(包含空白行)。
   主要是 . 逗點代表所有內容。

   findstr   "*"   c:\f.txt
   找不到任何資料,因為左邊沒有指定的字串條件。

 

   findstr   "1*"   c:\f.txt
   卻顯示全部內容。(1換成任何字串都是一樣) <--無法理解 orz…

 


   2.2 *號,無""包含著,為正則表達式作用,
                  表示*左側字串 或 表達式的重複次數。(零次或者多次)

   findstr   .*   c:\f.txt
     從文件c:\f.txt中,搜尋所有內容(包含空白行)。

3. ^ 表示行首
   findstr "^TEST" c:\f.txt

   找出符合該行開頭是 TEST 的內容,
   例:"TEST3"

4. $ 表示行尾
    findstr "ST3$" c:\f.txt
    找出符合該行結尾是ST3的內容,
    例:"TEST3"

5.findstr "[0-9]" c:\f.txt
   從文件c:\f.txt中,找出符合數字0-9之一的內容。
  
   findstr "[a-zA-Z]" c:\f.txt
   從文件c:\f.txt中,找出有英文字的內容。
  
   findstr "[ab]" c:\f.txt
   從文件c:\f.txt中,找出有 a 或 b 字母的內容。

   findstr "[a-fl-z]" c:\f.txt
   從文件c:\f.txt中,找出有 a-f 或 l-z 字母的內容。

   findstr "T[DEF][STU]T" c:\f.txt   -->太強了
   從文件c:\f.txt中,找出符合 TDST,TDTT,TDUT,TEST….等內容。

6.finstr "[^0-9]" c:\f.txt
   排除該行若每一字都是0-9。
   例如:該行為 135135全部都符合0-9規則,則排除不顯示,
              該行為  135A246A非0-9數字,則會顯示。
 

7.findstr "^[0-9]*$" c:\f.txt    

   []是符合在[]集合中的字元之一即是,有無全部都符合的作法?

   (1).找出該行全部都是數字。例:123,若是123A就不符合了
         Findstr "^[0-9]*$" c:\f.txt

 

   (2).找出該行全部都是大寫英文字母
         Findstr "^[a-z]*$" c:\f.txt


   以"^[0-9]*$"為例,可看成執行3個指令:^[0-9] [0-9]* [0-9]$
   第一個^[0-9] 表示行首要符合0-9
   第三個[0-9]$表示行尾要符合0-9

   -_-…不過我對第二個[0-9]* 答案沒有把握,我想表達的是
   前後除外,中間都符合0-9的字串。

   這樣就能理解這個複雜的指令是如何做到的了?

8. "\<" 及 "\>"

    這個表示精確搜尋一個字串,
    \<
xyz  表示需符合字的開始位置,
   
xyz \>表示需符合字的結束位置。
   
    例:
    echo hello my friend|findstr "\<friend\>" 
    答案:hello my friend

    echo hello myfriend|findstr "\<friend\>"
    答案:空白,找不到

               因為他要找的是 "friend"這個字串,所以不可以。

    echo hello myfriend|findstr ".*friend\>"
    答案:hello myfriend
               表示該字串前面不care,但後面需符合computer。


常用參數使用範例:
Syntax
  
FINDSTR [options][/F:file][/C:string][/G:file][string(s)]
            [pathname(s)]

Key
   string      Text to search for.
   pathname(s) The file(s) to search.

      /C:string   Use string as a literal search string.
      /B   Match pattern if at the Beginning of a line.
      /E   Match pattern if at the END of a line.
      /X   Print lines that match exactly.
      /V   Print only lines that do NOT contain a match.  
      /N   Print the line number before each line that matches.


 
測試資料(c:\f.txt) 
111
test1 test2 test3
test1 test1 test1

my friend
my
friend
4-119
5110
test

 


測試開始: 

C:\>FINDSTR "my friend" c:\f.txt
my friend
my
friend

C:\>FINDSTR /C:"my friend" c:\f.txt
my friend

C:\>FINDSTR /B /N /C:"test1" c:\f.txt
2:test1 test2 test3
3:test1 test1 test1

 

C:\>FINDSTR /B /N /C:"test" c:\f.txt
2:test1 test2 test3
3:test1 test1 test1
10:test

C:\>FINDSTR /B /X /C:"test" c:\f.txt

C:\>FINDSTR /B /X /C:"test" c:\f.txt

C:\>FINDSTR /B /V /C:"test" c:\f.txt
111

my friend
my
friend
4-119
5110

C:\>FINDSTR /B /M /C:"test" c:\f.txt
c:\f.txt


資料來源:
1.
Findstr - Search for strings

arrow
arrow
    全站熱搜

    味味A 發表在 痞客邦 留言(1) 人氣()