Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
995 views
in Technique[技术] by (71.8m points)

powershell - Show the previous and after N lines each for the matched lines?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...