LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to get the most occuring string from the logfile on LINUX (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-get-the-most-occuring-string-from-the-logfile-on-linux-4175615896/)

DusanM 10-18-2017 08:19 AM

How to get the most occuring string from the logfile on LINUX
 
Hello,

I am a newbie in LINUX and I want to ask how can I get the most occuring string from the file in LINUX.

I have a logfile on LINUX server and want to find out, which device and which log output is the most occuring.


Thank you

Turbocapitalist 10-18-2017 08:24 AM

Welcome.

There are several ways, but you'll have to be more specific about what data you have, what you want to find out, and what you have tried so far.

How familiar are you with pipes? You can pipe the output of sort into uniq. The -k option in the former and the -c and -f options in the latter might be of use if you are looking at whole lines. But if you want to trim some things from the line first with some fancier criteria, then you might need to look at sed or awk too.

Code:

man sort
man uniq

The manual pages are references, not tutorials. If you learn to combine them with web searches, you'll go far quickly.

jamison20000e 10-18-2017 09:04 AM

Hi.

I searched: Linux Log Files, found: https://www.cyberciti.biz/faq/linux-...ew-logs-files/ ;):hattip:

then, maybe: "find matching" text in Linux: http://www.sc.edu/beaufort/library/p...es/bones.shtml ‽ :Pengy::scratch:

...have fun! :D

DusanM 10-18-2017 02:54 PM

The thing is, that I have a text file (which is very huge - about 400MB) and I need to find out which from the following words occurs most often and in how many lines:

sw1, sw2, sw3, rt1, rt2, rt3

AwesomeMachine 10-18-2017 03:36 PM

You can use grep to count the number of occurrences.
Code:

$ grep -c sw1 log.file
If you want to you make a script to do every string without further interaction.

DusanM 10-19-2017 03:02 PM

Quote:

Originally Posted by AwesomeMachine (Post 5771422)
You can use grep to count the number of occurrences.
Code:

$ grep -c sw1 log.file
If you want to you make a script to do every string without further interaction.

OK, lets say, that sw1 occurs in the log.file 500 times.

1.Now I need to view ,the lines(outputs) 130-140 where the sw1 occurs in the log.file (something like segment).

2.And the last step is to find out how many times the strings "deny tcp" and "rt1" occur in the file log.file together.


Thank you,

lsalab 10-19-2017 04:04 PM

I believe you can do all that with awk:

Code:

man awk

TB0ne 10-19-2017 04:11 PM

Quote:

Originally Posted by DusanM (Post 5771770)
OK, lets say, that sw1 occurs in the log.file 500 times.

1.Now I need to view ,the lines(outputs) 130-140 where the sw1 occurs in the log.file (something like segment).
2.And the last step is to find out how many times the strings "deny tcp" and "rt1" occur in the file log.file together.

You misunderstand what you're getting told: you do not have to keep restating your question, we understand it.

What we do NOT see, though, is any effort on your part to do this work, but rather you asking us to do it for you. Read the "Question Guidelines" link in my posting signature. We are happy to help, but you need to do your part. Post what scripts/efforts of your own that you've tried, and tell us where you're stuck. Otherwise, there are LOTS of easily-found things about searching files in Linux, counting, etc., along with loads of bash scripting tutorials. There is no shortage of resources to help you make an effort.


All times are GMT -5. The time now is 02:24 AM.