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.

3

6 Answers

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 
1

"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> 

Look, the sed script that prints the 100 last lines you can find in the documentation for sed ():

$ 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:

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)

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 :

less cheatsheet :

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

0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy