Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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():
share|improve this question

2 Answers 2

up vote 2 down vote accepted

You can use this command:

$ awk '/2012\/04\/19/,/2012\/04\/22/' file.txt

You need to escape the slashes as shown.

Edit:

It can be done using variables like:

$ a='2012\/04\/19'
$ b='2012\/04\/22'
$ awk "/$a/,/$b/" file.txt
[ 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():

Edit2:

Escaping the slashes can be using done sed command like:

$ a='2012/04/19'
$ aa=$(echo $a | sed 's/\//\\\//g')
$ echo $aa
2012\/04\/19

Then, you can aa instead of a. Similarly, it can be done for b.

share|improve this answer
    
yes but I need the arguments they are dynamic and arguments can change - so I need to use in awk the "-v" flag please advice how to do that ? –  yael Apr 19 '12 at 7:44
    
@yael: updated my answer. –  Khaled Apr 19 '12 at 7:46
    
yes thx but I dont want to add the "\" in the argument because I take the string as 2012/04/19 from the file.txt , so did you have other solution ? –  yael Apr 19 '12 at 7:51
1  
@yael the \ is required because it escapes the / which would otherwise act as a delimiter in the awk program. This is very basic stuff that you should really know. Can I suggest that instead of getting SF to do your job for you, you speak to your manager about getting some basic training, that you learn to use man pages, and that you spend some time learning how to solve problems yourself. –  Iain Apr 19 '12 at 7:58
1  
you may use different separators for sed: sed 's|/|\/|g' –  jollyroger Apr 19 '12 at 11:09

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:

a='2012/04/19'
b='2012/04/22'
awk -v A="$a" -v B="$b" '$0 ~ A, $0 ~ B' file.txt

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.

share|improve this answer
    
WOW very cooooooooool –  yael Apr 19 '12 at 11:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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