How to display the content of the specified number of lines in a file in Linux


1. The grep command is better than the sed command
http://www.cnblogs.com/end/archive/2012/02/21/2360965.html
grep -n -C 100 "hello" a.log > searchResult.txt
above The command implements multiple functions:
1) Find the content of the keyword hello contained in the file a.log
2) Display the line number in the result
3) If the line containing hello is 30-60 lines, then -C 100, the display is 30+100 - 60+100, that is, the content of 130 to 160 lines. The advantage of this is that sometimes we can only initially locate the log range through keywords. The complete log may be several hundred lines before and after, so that for large files For example, for 2G, we can quickly locate all relevant logs of the problem.

2. tail/head/sed
reference link: http://jingyan.baidu.com/article/b24f6c823d9bae86bfe5daa5.html

1. tail -n +/- numeric filename

2, head -n numeric filename

3, sed -n " start line, end line p" filename

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326269948&siteId=291194637
Recommended