[LINUX] Search for character strings in files [Comparison between Bash and PowerShell]

Return to Bash and PowerShell command correspondence table

I just learned how to do PowerShell.


Bash

Command format

$ grep [option] <The character string you want to search> [file name]

# [option]: 任意(以下は、私がよく使うoption)
    -i:Search in case insensitive
    -I:Exclude binary files from search
    -n:Display the line number of the part that was caught in the search
    -r:Also search for files in subdirectories
    -v: <The character string you want to search>Show what does not match
    -w:Show only exact matches
    --exclude="*.extension": 指定したextensionは検索対象外にする
    --include="*.extension": 指定したextensionのみ検索対象にする

# <The character string you want to search>:Mandatory
If space is included in the middle, ""String string"Enclose in double quotes.

# [file name]:Optional (but close to mandatory)
Search only in the specified file.
fundamentally"*(Asterisk): All files can be searched.
I want to narrow down the search target (for example, hoge).Specify a specific file name only when (txt file only).

Example of use

Example 1) Search for all files in the current directory with hoge

$ grep hoge *

Example 2) Search for the hoge.txt file in the current directory with hoge

$ grep hoge hoge.txt

Example 3) Search for exact matches with main for all files including subdirectories

$ grep -rw main *

Example 4) Search for all files including subdirectories that do not contain main

$ grep -rv main *

Example 5) Search by color for all files other than json and xml files including subdirectories

$ grep -r --exclude="*.json" --exclude="*.xml" color *

PowerShell

Command format

> Select-String -Path <file name> -Pattern '<The character string you want to search>' [option]

# <file name>about
Search only in the specified file.
fundamentally"*(Asterisk): All files can be searched.
I want to narrow down the search target (for example, hoge).Specify a specific file name only when (txt file only).

# <The character string you want to search>about
Specify what you want to search here.
Be sure to "single quote (single quote)[Shift] + [7]) ”.

# [option]
    -Encoding:Character code (Shift_JIS and UTF-8 etc.)
    -NotMatch: <The character string you want to search>Show what does not match
    -Exclude <String>:In the file name<String>Exclude those containing
    -Include <String>:In the file name<String>Only those that include
> Get-ChildItem -Path <Folder path you want to search> -Filter <file name> -Recurse | Select-String -Pattern '<The character string you want to search>'

# -About Recurse
Attach this when you want to include subfolders in the search target.

# <Folder path you want to search>
Specify where to start the search.
Search from the current folder: .(Dot)
Search from home folder: ~(Tilde) or$HOME
Search from root folder: /(Slash)

# <file name>about
Same as "Search only in current folder" above.

# <The character string you want to search>about
Same as "Search only in current folder" above.

Example of use

Example 1) Search for all files in the current directory with hoge

> Select-String -Path * -Pattern 'hoge'

Example 2) Search for the hoge.txt file in the current directory with hoge

> Select-String -Path hoge.txt -Pattern 'hoge'

Example 3) Search for files containing main, including all files including subdirectories.

> Get-ChildItem -Path ./* -Filter * -Recurse | Select-String -Pattern 'main'

Example 4) Search for all files including subdirectories that do not contain main

> Get-ChildItem -Path ./* -Filter * -Recurse | Select-String -Pattern 'main' -NotMatch

Example 5) Search by color for all files other than all json files including subdirectories (multiple files are unknown)

> Get-ChildItem -Path ./* -Filter * -Recurse | Select-String -Pattern 'color' -Exclude *.json

Recommended Posts

Search for character strings in files [Comparison between Bash and PowerShell]
Search for strings in files
Create new file [Comparison between Bash and PowerShell]
Search for strings in Python
Recursively search for files and directories in Python and output
Creating an alias (when there are multiple arguments) [Comparison between Bash and PowerShell]
Evaluate and concatenate strings in variables using eval in bash
Performance comparison between 2D matrix calculation and for with numpy
3-3, Python strings and character codes
File open function in Python3 (difference between open and codecs.open and speed comparison)
Difference between list () and [] in Python
Manipulate files and folders in Python
Speed comparison between CPython and PyPy
Export and output files in Python
Vectorize sentences and search for similar sentences
Extract strings from files in Python
[Python] Summary of conversion between character strings and numerical values (ascii code)
UnionFind in python (enhanced version: strings and tuples are allowed for elements)