Retrieve last 100 lines logs

LinuxLoggingSed

Linux Problem Overview


I need to retrieve last 100 lines of logs from the log file. I tried the sed command

sed -n -e '100,$p' logfilename

Please let me know how can I change this command to specifically retrieve the last 100 lines.

Linux Solutions


Solution 1 - Linux

You can use tail command as follows:

tail -100 <log file>   > newLogfile

Now last 100 lines will be present in newLogfile

EDIT:

More recent versions of tail as mentioned by twalberg use command:

tail -n 100 <log file>   > newLogfile

Solution 2 - Linux

"tail" is command to display the last part of a file, using proper available switches helps us to get more specific output. the most used switch for me is -n and -f

SYNOPSIS

> tail [-F | -f | -r] [-q] [-b number | -c number | -n number] [file ...]

Here

> -n number : > The location is number lines. > > -f : The -f option causes tail to not stop when end of file is > reached, but rather to wait for additional data to be appended to the > input. The -f option is ignored if the > standard input is a pipe, but not if it is a FIFO.

Retrieve last 100 lines logs

To get last static 100 lines  
     tail -n 100 <file path>

To get real time last 100 lines
     tail -f -n 100 <file path>

Solution 3 - Linux

Look, the sed script that prints the 100 last lines you can find in the documentation for sed (https://www.gnu.org/software/sed/manual/sed.html#tail):

$ cat sed.cmd
1! {; H; g; }
1,100 !s/[^\n]*\n//
$p

$ sed -nf sed.cmd logfilename

For me it is way more difficult than your script so

tail -n 100 logfilename

is much much simpler. And it is quite efficient, it will not read all file if it is not necessary. See my answer with strace report for tail ./huge-file: https://unix.stackexchange.com/questions/102905/does-tail-read-the-whole-file/102910#102910

Solution 4 - Linux

You can simply use the following command:-

tail -NUMBER_OF_LINES FILE_NAME

e.g tail -100 test.log

  • will fetch the last 100 lines from test.log

In case, if you want the output of the above in a separate file then you can pipes as follows:-

tail -NUMBER_OF_LINES FILE_NAME > OUTPUT_FILE_NAME

e.g tail -100 test.log > output.log

  • will fetch the last 100 lines from test.log and store them into a new file output.log)

Solution 5 - Linux

I know this is very old, but, for whoever it may helps.

less +F my_log_file.log

that's just basic, with less you can do lot more powerful things. once you start seeing logs you can do search, go to line number, search for pattern, much more plus it is faster for large files.

its like vim for logs[totally my opinion]

original less's documentation : https://linux.die.net/man/1/less

less cheatsheet : https://gist.github.com/glnds/8862214

Solution 6 - Linux

len=`cat filename | wc -l`
len=$(( $len + 1 ))
l=$(( $len - 99 ))
sed -n "${l},${len}p" filename

first line takes the length (Total lines) of file then +1 in the total lines after that we have to fatch 100 records so, -99 from total length then just put the variables in the sed command to fetch the last 100 lines from file

I hope this will help you.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSurabhiView Question on Stackoverflow
Solution 1 - LinuxSteephenView Answer on Stackoverflow
Solution 2 - LinuxAmitesh BhartiView Answer on Stackoverflow
Solution 3 - Linuxuser184968View Answer on Stackoverflow
Solution 4 - LinuxArun Kumar NView Answer on Stackoverflow
Solution 5 - LinuxmdsadiqView Answer on Stackoverflow
Solution 6 - LinuxaxyView Answer on Stackoverflow