0

I have a Error Log file with contents as -

2017/11/06 13:17:05 [notice] 18164#18164: signal process started
.
.
.

I have command that will extract the date and notice,warn message

cat  whole_error.log | cut -d" " -f1,3,5-20 | sort -nr | grep "warn\|notice" | awk '$1 >= 2017/09/02 && $1 <= 2017/11/06' | awk '{print $1 " " $2" " $3}'

Its working fine entirely, i am getting the expected output But, i want to take the start date and end date as command line argument input and for that, i wrote the script as -

#!/bin/bash
file_name=$1
start_date=$2
end_date=$3


cat  $file_name | cut -d" " -f1,3,5-20 | sort -nr | grep "warn\|notice" | awk '$1 >= $start_date && $1 <= $end_date' | awk '{print $1 " " $2" " $3}'

But its not returning anything. No error message nothing..Just the prompt arrives again. How to fix this..

  • 2
    That whole command line could be replaced with just one invocation of `awk`. No need for `cat`, `cut`, `sort` *or* `grep`. – Charles Duffy Nov 22 '17 at 13:49
  • Also, see http://shellcheck.net/ -- that correctly detects that `start_date` and `end_date` are unused (providing a big hint about the problem), and also picks up on the useless `cat` and the bugs caused by lack-of-quoting. – Charles Duffy Nov 22 '17 at 13:52

1 Answers1

1

Use -v to pass shell variables into awk:

#!/bin/bash

file_name=$1
start_date=$2
end_date=$3

<"$file_name" cut -d" " -f1,3,5-20 \
  | sort -nr \
  | awk -v start_date="$start_date" -v end_date="$end_date" \
      '/warn|notice/ && $1 >= start_date && $1 <= end_date {print $1 " " $2" " $3}'
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • awk -v start_date="$2" -v end_date="$3" "line change here" cat $file_name | cut -d" " -f1,3,5-20 | sort -nr | grep "warn\|notice" | awk '$1 >= $start_date && $1 <= $end_date' | awk '{print $1 " " $2" " $3} , implemented like this, still not working – Shivam Srivastava Nov 22 '17 at 14:02
  • Of course that isn't working. The `-v` arguments need to be passed to the same `awk` instance that uses the values. And see how I changed the `awk` code -- it's `start_date`, not `$start_date` inside the script. – Charles Duffy Nov 22 '17 at 14:03
  • Then, how should i pass the arguments to the shell script, back to the original question – Shivam Srivastava Nov 22 '17 at 14:04
  • The same way you already do -- my original code was intended to be a like-for-like replacement to the `grep` and `awk` components, leaving the rest the same. – Charles Duffy Nov 22 '17 at 14:12
  • Thank you, worked for me, but was finiing hard to get the essence of awk. :) .. Hope to learn soon, any references for learning awk will be great. – Shivam Srivastava Nov 22 '17 at 14:23
  • The [`awk` tag wiki](https://stackoverflow.com/tags/awk/info) has links to several recommended books; I believe one of them is also published as the manual at https://www.gnu.org/software/gawk/manual/html_node/index.html – Charles Duffy Nov 22 '17 at 14:34