0

I have a code like below:

$ awk -F'[]]|[[]' \
  '$0 ~ /^\[/ && $2 >= "2014-04-07 23:00"    { p=1 }
   $0 ~ /^\[/ && $2 >= "2014-04-08 02:00:01" { p=0 }
                                           p { print $0 }' log

Here I want to assign "2014-04-07" in one variable "23:00" in another variable and so on. And get the input from their values ( using $var or something like that ). Can someone help me to modify the above code so I can use variable instead hardcording the timestamp.

Below is the link for original question How to extract logs between two time stamps.

1

Please try reading some documentation before posting here. What you're asking is trivial to find with a 5 minute google search. You might want to read through our Help page on how to ask questions to get a better idea of how this site works.

Anyway, you can either pass variables as arguments using the -v option:

awk -F'[]]|[[]' -v var1="2014-04-07 23:00" -v var2="2014-04-08 02:00:01" \
  '$0 ~ /^\[/ && $2 >= var1 { p=1 }
   $0 ~ /^\[/ && $2 >= var2 { p=0 }
   p { print $0 }' log

Or you can set the variables in a BEGIN block:

awk -F'[]]|[[]' \
  'BEGIN{var1="2014-04-07 23:00"; var2="2014-04-08 02:00:01"}
   $0 ~ /^\[/ && $2 >= var1 { p=1 }
   $0 ~ /^\[/ && $2 >= var2 { p=0 }
   p { print $0 }' log
  • I think this link to google is more appropriate in this case. – user61786 Apr 14 '14 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.