please advice what wrong with my awk syntax and how to fix it ( this syntax is in my ksh script ) and I run my script on linux machine
my target is to get from the file.txt only the lines that between the dates:
FROM 2012-04-19 TO 2012-04-22
remark - other solution can be with perl
[root@test1 /var/tmp]# a='2012/04/19'
[root@test1 /var/tmp]# b='2012/04/22'
[root@test1 /var/tmp]# awk -v A=$a -v B=$b '/A/,/B/' file.txt
awk: syntax error near line 1
awk: bailing out near line 1
file.txt
[ 2012/04/18 21:49:01:857 ] Monitor::handle_client_message():
[ 2012/04/18 21:50:02:379 ] Monitor::handle_client_message(
[ 2012/04/18 21:57:52:64 ] Monitor::handle_client_message():
[ 2012/04/18 21:57:52:252 ] Monitor::handle_client_message(
[ 2012/04/18 21:58:46:958 ] Monitor::handle_client_message():
[ 2012/04/19 21:58:46:958 ] Monitor::handle_client_message():
[ 2012/04/20 21:58:46:958 ] Monitor::handle_client_message():
[ 2012/04/21 21:58:46:958 ] Monitor::handle_client_message():
[ 2012/04/22 21:58:46:958 ] Monitor::handle_client_message():
You can use this command:
You need to escape the slashes as shown.
Edit:
It can be done using variables like:
Edit2:
Escaping the slashes can be using done
sed
command like:Then, you can
aa
instead ofa
. Similarly, it can be done forb
.The following is the correct way to do what you want.
You don't want to have to pre-escape your search patterns and you should use the
-v
option as you show in your question (but the shell variables should be quoted).When you have patterns in variables, you can't use
//
to surround the patterns - you have to use the match operator~
(tilde).This works correctly for your data:
The reason you can't put variables inside
//
is that the characters in the name of the variable are seen as the pattern rather than the contents of the variable.