1

I have a simple function which will give me past one hour log

past_hour_log(){
    awk -v Date1=$(date -d"now 1 hour ago" "+%H:%M:%S") -v Date2=$(date -d"now 1 hour ago" "+%d") ' { if ($3 > Date1 && $2 >= Date2) {print $0}} ' /var/log/syslog
}

My function Output :

Oct  9 12:15:15 localhost-IdeaPad-980 systemd[1]: time has been changed
Oct  9 12:16:00 localhost-IdeaPad-980 systemd[1534]: time has been changed
Oct  9 12:17:00 localhost-IdeaPad-980 systemd-timesyncd[25237]: Synchronized to time server 91.189.91.157:123 (ntp.ubuntu.com).
Oct  9 12:17:01 localhost-IdeaPad-980 CRON[27685]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Oct  9 12:17:01 localhost-IdeaPad-980 CRON[29613]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)

So here on next line, I am trying to pass this function output into grep xargs to get a total count of the string given as input from input.txt

TIME_COUNT="$(cat input.txt | xargs -I{} grep {} past_hour_log | awk '{print $7}' | awk -F '?' '{print $1}' | uniq -c)"

But the output shows me below. Are there any ideas to achieve this?

grep: past_hour_log: No such file or directory

7
  • @Inian: Kindly understand the description given and mark as duplicate. Commented Oct 9, 2018 at 7:14
  • Your requirement is an exact duplicate of the one I've given. Just export your function as export -f past_hour_log before calculating TIME_COUNT Commented Oct 9, 2018 at 7:16
  • I try export but the error i get 'export: Illegal option -f' Commented Oct 9, 2018 at 7:29
  • I need some exact solution for my question how this can be used. I am new to shell. Commented Oct 9, 2018 at 7:34
  • First of all, I see some serious quoting problems in your function past_hour_log. Could you fix those first and come back to us? Commented Oct 9, 2018 at 12:37

1 Answer 1

1

You might want to attempt the following:

past_hour_log() {
    date1=$(date -d"now 1 hour ago" "+%H:%M:%S")
    date2=$(date -d"now 1 hour ago" "+%d")
    awk -v Date1="$date1" -v Date2="$date2" '($3 > Date1 && $2 >= Date2){print}' /var/log/syslog;
}

and then do

TIME_COUNT="$(past_hour_log | grep -F -f input.txt | awk '{print $7}' | awk -F '?' '{print $1}' | uniq -c)"
Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.