Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

The following command will show the lines match the pattern.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.0k views
Welcome To Ask or Share your Answers For Others

1 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.

select-string -path file.txt -pattern "..." -Context 1,3

The file name and line number will be included in the output


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...