Home » Questions » Computers [ Ask a new question ]

taking certain lines from log files on linux

taking certain lines from log files on linux

i have log files in the current structure:

Asked by: Guest | Views: 248
Total answers/comments: 1
Guest [Entry]

"Let's say that all your files are under one directory, for example: /var/www/html/logs and it's subdirectory. Let's assume that all log files end with "".log"". Let's assume that you want the combined log to /home/username/combinedlog.txt

There is a unix-tool called ""find"" that searches for files in given directory tree, for example

find /var/www/html/logs -name '*.log' -type f -print

would print names of all files ending with '.log' from directory /var/www/html/logs and it's subdirectories.

As the previous answers show, you can get just the OS with ""grep OS:|tail -c +4"" and just the Browser with ""grep OS:|tail -c +4"". The first answer also shows how to make for-loop with results of one command. So the combined answer would be:

for i in `find /var/www/html/logs -name '*.log' -type f -print`; do
grep ""OS:"" $i |tail -c +4 >> /home/username/combinedlog.txt;
grep ""Browser:"" $i|tail -c +9 >> /home/username/combinedlog.txt;
done;

And this would go through all files at once. Note that "">>"" is append to a file, if you run this again without removing the previous combined log file, you will duplicate the contents!

Edit: or you could use egrep + awk from the previous answer and replace the two greps with one egrep + awk. You could also use ""cut"" instead of ""tail"" or ""awk""."