awk and shell expansion
hello:
I have a problem that I've solved but I'd like to better understand why it worked. The particulars are as follows: I have a log_file of the following form: ++++++++++ data data data data 2011-07-27 data data ++++++++++ data data 2011-07-28 data ++++++++++ "++++++++++" ---> is my record separator Objective: mail the record for a specific date. Approach: #!/bin/bash i=`date +%F` awk 'BEGIN{FS="\n"; RS="++++++++++"} /'$i'/ {print $0}' log_file >/tmp/alert mail -s "mydata" root@localhost < /tmp/alert In my inbox I find data data 2011-07-28 data Excellent. Now, trying to do things in one fell swoop why wouldn't the following work? awk 'BEGIN{FS="\n"; RS="++++++++++"; "date +F%"|getline dateVal} /dateVal/ {print $0}' log_file >/tmp/alert Can someone help me understand: 1. Why this second option does not work? 2. How awk can actually use the correct value for $i in the first script when it is enclosed in single quotes? I previously thought that single quotes kill any and all special meaning. This is as literal as you can get? How is the expansion working correctly in this case? Is there something special about using the / /, which is specific to regex? Thanks. |
First off ` /= '. A back tick (`) which is part of the tilde (~) key above the tab key is not a single quote ('). You are right in saying that single quotes change the meaning of a command where echo "$PATH" and echo '$PATH' give two different outputs.
I'm no awk expert but I don't believe you can simply pipe the output of a command like that from within awk. To execute commands you have to use the system function for example system("date"). As for the rest I'll defer someone else who would know better. By the way in your awk command the $i is not enclosed by single quotes. It is outside of them. Below I have highlighted the quoted text. awk 'BEGIN{FS="\n"; RS="++++++++++"} /'$i'/ {print $0}' log_file >/tmp/alert You might be able to do something like this. Code:
#!/bin/bash Code:
awk 'BEGIN{FS="\n"; RS="++++++++++"} /'`date +%F`'/ {print $0}' log_file | mail -s "mydata" root@localhost |
Quote:
|
Quote:
Code:
awk ' |
Thank you everyone for your timely replies. I really appreciate it.
|
Quote:
|
Quote:
|
Quote:
Code:
awk BEGIN{FS="\n"; RS="++++++++++"} /2011-07-29/ {print $0} log_file |
Quote:
|
Quote:
|
All times are GMT -5. The time now is 01:00 AM. |