LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-18-2019, 02:17 AM   #1
danykallery
LQ Newbie
 
Registered: Oct 2006
Location: Kerala, India
Distribution: RedHat, Fedora, Cent OS
Posts: 26

Rep: Reputation: 0
sample script to get report from the last 7 days.


I have one server where the program logging the access count in one log format and it creates on each day.
the task is i want to run a script in which need to extract the hit count from each of the day.

audit.log
audit.log_date.gz
audit.log_2ndday.gz

these are the syntax of the log file. from each log (for the last 7 days) i need to extract the count. and combine it.

zcat audit.log | grep -e "url_name" > siteone_count

Replies would be appreciated...needed urgent help
 
Old 02-18-2019, 02:25 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Ok. The urgency is on your side, not on the rest of us who volunteer here.

Either perl or awk will be a good match for your task.

However, what have you tried so far and where are you stuck? We can walk you through the hard parts. Please provide samples of your code and a few lines of sample input.
 
1 members found this post helpful.
Old 02-18-2019, 03:35 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by danykallery View Post
zcat audit.log | grep -e "url_name" > siteone_count
why zcat? have you actually tried that? audit.log would not aoppear to be zipped (or archived).
and what's wrong with that approach? if that works, you just have to count the lines, no?
 
Old 02-18-2019, 04:31 AM   #4
danykallery
LQ Newbie
 
Registered: Oct 2006
Location: Kerala, India
Distribution: RedHat, Fedora, Cent OS
Posts: 26

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Turbocapitalist View Post
Ok. The urgency is on your side, not on the rest of us who volunteer here.

Either perl or awk will be a good match for your task.

However, what have you tried so far and where are you stuck? We can walk you through the hard parts. Please provide samples of your code and a few lines of sample input.
Yes Understood. It would be helpful if i get an answer asap. That is what i meant.

i am confused to write a script because each log has generated as below

access_log.2019-02-16
access_log.2019-02-17
access_log.2019-02-18

and getting access count individually, i am not sure how should i write script and it needs only last 7 days too.
 
Old 02-18-2019, 04:33 AM   #5
danykallery
LQ Newbie
 
Registered: Oct 2006
Location: Kerala, India
Distribution: RedHat, Fedora, Cent OS
Posts: 26

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ondoho View Post
why zcat? have you actually tried that? audit.log would not aoppear to be zipped (or archived).
and what's wrong with that approach? if that works, you just have to count the lines, no?
Thanks for your response.

Some of the files are .gz. zcat is used for monthly report generation. any log file above 10 days old are converting as .gz.
 
Old 02-18-2019, 04:36 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
You can only use zcat on gzipped files and cat on plain text.

But about the main question, do you have a list of files which include a date in the name and you would like to get the latest seven of those file names?

If each file's time stamp matches the name, the utility ls has option -t and -r which might be useful if combined with head.

Otherwise you will have to parse the name, in awk or perl.

Again, what have you tried and where are you stuck?

Last edited by Turbocapitalist; 02-18-2019 at 04:42 AM.
 
Old 02-18-2019, 10:51 AM   #7
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Maybe something like the following?
Code:
find /path/to/logs -name "access_log*" -mtime -8 -print0 | xargs -0 grep "<url_name>" | wc -l
 
Old 02-19-2019, 12:29 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Or a loop around date using command substitution to build the file names:

Code:
date=$(date --date="$day day ago" +"%F");
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why is /sbin/initscript.sample not /etc/initscript.sample ? Lockywolf Slackware 4 06-16-2018 06:47 AM
Get first day of last month and last day of last month in bash xowl Linux - Software 18 02-09-2017 09:49 AM
script that extracts last 7 days of a log file johnmccarthy Linux - Newbie 10 07-03-2013 03:07 PM
In need of command or script to grep events for last n days cmartz Linux - Server 7 01-15-2013 01:20 AM
How do I grep my /var/log/secure file for the past 7 days or so many days? johnmccarthy Linux - Newbie 5 01-04-2013 09:43 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration