View the content of the lines before and after the keyword in the file
Sometimes the files are so large that we can't read them all to find what we want. At this time, we need a linux command to view the contents of a few lines before and after a keyword: grep
Using the help command of linux, as shown in the figure below, we can see the usage of grep. Here we pay attention to the display problem before and after the keyword.
Take the file test.txt as an example,
1) Display the content of ten lines after the line where the keyword (eg: 16:55) is located: cat test.txt | grep -A10 16:55 (11 lines are displayed in total, the first line is the line where the keyword is located)
2) Display the content of ten lines before the line where the keyword (eg: 16:55) is located: cat test.txt | grep -B10 16:55 (11 lines are displayed in total, the 11th line is the line where the keyword is located)
3) Display the content of ten lines before and after the line where the keyword (eg: 16:55) is located: cat test.txt | grep -C10 16:55 (21 lines are displayed in total, the 11th line is the line where the keyword is located)
0 Comments