SED variable substitution

Hi Chaps...

I have a log file as below:-

01 Oct 2009 12:57:56 DEBUG [pollingThread]:

01 Oct 2009 12:14:21 DEBUG [pollingThread]:.....
.
.
.
.05 Oct 2009 14:31:56 DEBUG [pollingThread]:....
.
.
.05 Oct 2009 12:57:56 DEBUG [pollingThread]:....
 
06 Oct 2009 01:23:11 DEBUG [pollingThread]:....
.
.
.06 Oct 2009 12:53:46 DEBUG [pollingThread]:....
 

Now I have to cut all the logs whcih are created today and do some basic operation...

now considering my end string time stamp will vary I have cut the time stamp of last line to a variable...

and start string can be taken as date as I have date in my log...

for cutting the log file i have written ..

 
#!/bin/bash
 
. ~/.profile
 
date_var=`date "+%d %h %Y"`
#date_var='01 Oct 2009'
time_stamp=`tail -1 Interface.log | cut -f4 -d " "`
end_string="$date_var $time_stamp"
sed '/^'$date_var'/,/^'$end_string'/!d' Interface.log | sed '/^#/d' | sed '$d' >> current_day_log.txt

when I run above script I get error as sed: command garbled: /^06

looks like there is a problem while substituting the varibale in SED...can anybody point out what I am doing wrong here...

Many Thanks in advance...

use "" instead of ''

sed "/^$date_var/,/^$end_string/!d"

BR

You have a whitespaces in you $date_var, so

sed '/^'$date_var'/,/^'$end_string'/!d' Interface.log | sed '/^#/d' | sed '$d' >> current_day_log.txt

is seen by the shell as (example)

sed /^01 Oct 2009/,/^01 Oct 2009 12:14:21/!d Interface.log | sed /^#/d | sed $d >> current_day_log.txt

This means that the sed command isn't seen as one parameter, but several, breaking the structure/command.

Better write it as

sed '/^'"$date_var"'/,/^'"$end_string"'/!d' Interface.log | sed '/^#/d' | sed '$d' >> current_day_log.txt

so that the whitespaces aren't seen as parameter delimiters, without running into parameter expansion problems.

The reason for your problem is that your variables contain spaces and a space character is interpreted by the shell as a field separator. Consider the following:

command arg1 arg2

The shell will treat "arg1" and "arg2" as different arguments. If you want these to be one argument of the form "arg1 arg2" you will have to use quotation marks to explain that to the shell:

command "arg1 arg2"

Here is your corrected script. Notice the differences (btw. i have shortened the superfluous pipeline into one sed invocation):

#!/bin/bash
 
. ~/.profile
 
date_var="$(date "+%d %h %Y")"
#date_var='01 Oct 2009'
time_stamp="$(tail -1 Interface.log | cut -f4 -d' ')"
end_string="$date_var $time_stamp"
sed -n '/^'"$date_var"'/,/^'"$end_string"'/ {;/^#/d;p;}' Interface.log  >> current_day_log.txt

I hope this helps.

bakunin

cheers!!!! mate