LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-29-2018, 06:48 AM   #16
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,805

Rep: Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206

Some improvement proposals:
1.
grep takes newline-separated patterns.
2.
It's more efficient to redirect the whole loop; further it gives you the choice between
done > filename and
done >> filename.
3.
< filename ... is more efficient than
cat filename | ...
4.
cd once and make sure it was successful!
5. Quotes around $var[@] and $@ keep the elements but prevent further word splitting and glob-expansion.
Code:
last7days=$(
  date +%Y-%m-%d -d "7 day ago"
  date +%Y-%m-%d -d "6 day ago"
  date +%Y-%m-%d -d "5 day ago"
  date +%Y-%m-%d -d "4 day ago"
  date +%Y-%m-%d -d "3 day ago"
  date +%Y-%m-%d -d "2 day ago"
  date +%Y-%m-%d -d "1 day ago"
  date "+%Y-%m-%d"
)
...
cd $folder || exit
for val in "${StringArray[@]}"
do
  for filename in ./*_${timestamp}.xml
  do
### below statement seeks existence of each element in StringArray
# if found, pipe to determine if changed in the last 7 days. pipe to outfile if meets all criteria
## 7-day capture
    < "$filename" grep -A1 "$val" | grep -B2 "$last7days"
  done
done >> $outfile
 
Old 12-29-2018, 09:40 PM   #17
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,375

Rep: Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754
Just for fun, a solution that uses gawk so that the 'mktime' function can be used to handle time stamps with greater flexibility. Also it saves multiple reads of the log files for each user.
Code:
#!/bin/bash

## Date setup for feeding into the process

start_date=$(date +%Y-%m-%d -d "7 day ago")
start_time="00:00:00"
end_date=$(date "+%Y-%m-%d")
end_time="23:59:59"

prevday=`date -d yesterday '+%Y%m%d'`
## remove the previous days file - housekeeping to reduce file buildup
#rm /tmp/svnaudit/svnaudit_$prevday.txt

timestamp=`date +%Y%m%d`

#### create file/dir variables
#folder=/mnt/midtier_logs/report/Audit
#outfile=/tmp/svnaudit/svnaudit_$timestamp.txt
outfile="outfile.txt"
#emailfile=/tmp/svnaudit/svnaudit_emailme.txt
#rm $emailfile

#cd $folder || exit

for filename in ./*_${timestamp}.xml; do
  gawk -v start_date=$start_date -v start_time=$start_time -v end_date=$end_date -v end_time=$end_time '
    BEGIN {
    FIELDWIDTHS="6:10 1:8"
    split(start_date,d,"-")
    split(start_time,t,":")
    st=mktime(d[1]" "d[2]" "d[3]" "t[1]" "t[2]" "t[3])
    split(end_date,d,"-")
    split(end_time,t,":")
    et=mktime(d[1]" "d[2]" "d[3]" "t[1]" "t[2]" "t[3])
    }
    /revision=/ {rev=$0}
    /user1|user12|user30|dev1|dev5|dev25|dev15|dev4/ {aut=$0}
    /[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}/ {
      split($1,d,"-")
      split($2,t,":")
      ct=mktime(d[1]" "d[2]" "d[3]" "t[1]" "t[2]" "t[3])
      if (ct > st && ct < et) {
        print rev
        print aut
        print
      }
    }' "$filename" >> "$outfile"
done
Notes:
1. I have used the FIELDWIDTHS variable to make it easy to isolate the date and time from the log file, as the line seems to be consistently created in the example data.
2. I have commented out the local file handling.

Last edited by allend; 12-29-2018 at 09:43 PM.
 
Old 12-29-2018, 10:01 PM   #18
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,135

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
Quote:
Originally Posted by allend View Post
Also it saves multiple reads of the log files for each user.
I used to worry about such things, but for most (sane) sized files, this is no longer an issue - if the entire file remains RAM resident in the page-cache, no (extra) disk I/O ensues.
 
Old 12-30-2018, 12:48 AM   #19
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,805

Rep: Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206
Quote:
Originally Posted by allend View Post
Just for fun, a solution that uses gawk so that the 'mktime' function can be used to handle time stamps with greater flexibility. Also it saves multiple reads of the log files for each user.
Code:
#!/bin/bash

## Date setup for feeding into the process

start_date=$(date +%Y-%m-%d -d "7 day ago")
start_time="00:00:00"
end_date=$(date "+%Y-%m-%d")
end_time="23:59:59"

prevday=`date -d yesterday '+%Y%m%d'`
## remove the previous days file - housekeeping to reduce file buildup
#rm /tmp/svnaudit/svnaudit_$prevday.txt

timestamp=`date +%Y%m%d`

#### create file/dir variables
#folder=/mnt/midtier_logs/report/Audit
#outfile=/tmp/svnaudit/svnaudit_$timestamp.txt
outfile="outfile.txt"
#emailfile=/tmp/svnaudit/svnaudit_emailme.txt
#rm $emailfile

#cd $folder || exit

for filename in ./*_${timestamp}.xml; do
  gawk -v start_date=$start_date -v start_time=$start_time -v end_date=$end_date -v end_time=$end_time '
    BEGIN {
    FIELDWIDTHS="6:10 1:8"
    split(start_date,d,"-")
    split(start_time,t,":")
    st=mktime(d[1]" "d[2]" "d[3]" "t[1]" "t[2]" "t[3])
    split(end_date,d,"-")
    split(end_time,t,":")
    et=mktime(d[1]" "d[2]" "d[3]" "t[1]" "t[2]" "t[3])
    }
    /revision=/ {rev=$0}
    /user1|user12|user30|dev1|dev5|dev25|dev15|dev4/ {aut=$0}
    /[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}/ {
      split($1,d,"-")
      split($2,t,":")
      ct=mktime(d[1]" "d[2]" "d[3]" "t[1]" "t[2]" "t[3])
      if (ct > st && ct < et) {
        print rev
        print aut
        print
      }
    }' "$filename" >> "$outfile"
done
Notes:
1. I have used the FIELDWIDTHS variable to make it easy to isolate the date and time from the log file, as the line seems to be consistently created in the example data.
2. I have commented out the local file handling.
Again, use efficient redirection (especially if you want to reduce I/O operations!)
Code:
    }' "$filename"
done >> "$outfile"
 
Old 12-30-2018, 03:06 AM   #20
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,916

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
do not need the for loop
Code:
awk -v .... ' script ' ./*_${timestamp}.xml >> "$outfile"
 
1 members found this post helpful.
Old 12-31-2018, 05:01 AM   #21
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,805

Rep: Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206
... unless you want to handle zero matches or thousands of matches (prevent from "awk: two many arguments").
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
delete lines between string patterns (inclusive) only when the lines within this range don't contain a certain string vincix Programming 10 01-08-2019 04:42 AM
LXer: How To Empty a File, Delete N Lines From a File, Remove Matching String From a File, And Remove Empty/Blank Lines From a File In Linux LXer Syndicated Linux News 0 11-22-2017 12:30 PM
[SOLVED] Will receive two file on same date but i need to extract only one file with date like spatil20 Linux - General 7 07-01-2015 01:16 AM
[SOLVED] extract lines using date range veda92 Programming 2 07-22-2013 05:58 AM
[SOLVED] Search within a log file within a time Range tonan Linux - Newbie 5 08-25-2011 03:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:47 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