Display Python Packages whose Name Includes Certain Text: PowerShell commands findstr
, Select-String
, and pipeline operator |
If we want to display Python packages whose name includes certain text, like “torch”, we can use following command:
1
pip list | findstr "torch"
1
2
3
4
5
torch 2.3.1+cu121
torchaudio 2.3.1+cu121
torchinfo 1.8.0
torchsummary 1.5.1
torchvision 0.18.1+cu121
where command findstr
1 is to:
Searches for patterns of text in files.
or use native PowerShell command (i.e. cmdlet) Select-String
2, which is more powerful and suitable for PowerShell:
1
pip list | Select-String "torch"
1
2
3
4
5
torch 2.3.1+cu121
torchaudio 2.3.1+cu121
torchinfo 1.8.0
torchsummary 1.5.1
torchvision 0.18.1+cu121
The Select-String
cmdlet uses regular expression matching to search for text patterns in input strings and files. You can use Select-String
similar to grep
in UNIX or findstr.exe
in Windows.
Select-String
is based on lines of text. By default, Select-String
finds the first match in each line and, for each match, it displays the file name, line number, and all text in the line containing the match. You can direct Select-String
to find multiple matches per line, display text before and after the match, or display a Boolean value (True or False) that indicates whether a match is found.
Select-String
can display all the text matches or stop after the first match in each input file. Select-String
can be used to display all text that doesn’t match the specified pattern.
You can also specify that Select-String
should expect a particular character encoding, such as when you’re searching files of Unicode text. Select-String
uses the byte-order-mark (BOM) to detect the encoding format of the file. If the file has no BOM, it assumes the encoding is UTF8.
In above two cases, symbol |
is the pipeline operator3, which is used to take the output of previous command to next command as the input:
A pipeline is a series of commands connected by pipeline operators (|
) (ASCII 124). Each pipeline operator sends the results of the preceding command to the next command.
The output of the first command can be sent for processing as input to the second command. And that output can be sent to yet another command [like Command-1 | Command-2 | Command-3
]. The result is a complex command chain or pipeline that’s composed of a series of simple commands.
…
In a pipeline, the commands are processed in order from left to right. The processing is handled as a single operation and output is displayed as it’s generated.
References