سؤال

I have a file with about 30.000.000 lines (Radius Accounting) and I need to find the some pattern from down to up, because I need the last result.

The command:

tac accounting.log | grep $pattern

gives what I need, but it's too slow because at first the OS has to read the whole file and then send to pipe.

So, I need something fast that read the file from the last line to first.

Thanks and sorry my English.

هل كانت مفيدة؟

المحلول

@anubhava's answer is a great in that if you know the pattern will be in the last X lines you can use tail -n X. However if you don't know the X value you have a couple of options.

1) Instead of trying to modify the tac command, modify the grep command to only match the first (this saves computational time because grep doesn't have to process the whole file).

tac accounting.log | grep -m 1 $pattern

2) Write a for loop that processes the file line by line backwards and then breaks from that for loop once you have found what you want.

نصائح أخرى

Instead of tac you can use tail -r.

For example use this command to read last 100 lines backwards and find patterns using grep:

tail -n 100 -r accounting.log | grep "$pattern"
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top