Question:
The following command will show the lines match the pattern.
1 2 |
cat file.txt | select-string -pattern "..." |
How to show the previous and after 3 lines each for the matched lines? (Like the -C3
parameter of grep.) Also is it possible to print out the line number?
BTW, the command is much slower comparing with grep?
Answer:
Specify the filename in the -Path
parameter of Select-String
, and use the -Context
switch to specify the number of lines before and after to include.
1 2 |
select-string -path file.txt -pattern "..." -Context 1,3 |
The file name and line number will be included in the output