Problem with sed

Hi Gurus

I am writing a script to capture conents of a file starting from a regular expression till end of the file. When I execute sed -n '/2010/,$p' filename
I can get correct output. Now I wanted date part so I did this

sed -n '/2010\/04\/27/,$p' filename

Everything is fine till here. But as soon as I try to put the date in variable and use with sed it doest work. The date variable will not have characters escaped. My question is how can I put date in a variable and use sed to capture content of file starting from the date till end of the file

for example var1=2010/04/27

now i want to print content of my file starting $var1 till end of file ..please help

Thanks in advance
perl

This should work:

var='2010\/04\/27'
sed -n '/'$var'/,$p' filename

Afaik following should also work:

var1="2010/04/27"
sed -n ''/$var1/,$p'' filename

This can't work because $var1 contains the regexp delimiter.

That's right! Thank you for your hint. I just was too fast and did not test it, like I normally use to. :rolleyes:

Thanks guys but i still have a problem. The value of variable var1 in the given example is not known and can differ with each execution. This variable will have a date value which will be something like var1="2010/04/27"

Therefore I can not use escape charcter since value will not be known.
Is there any way i can use delimeter other than / in sed ? i tried with may characters but it doesnt work.

thanks in advance

I read about delimiteres prior to posting here and also tried but unfortunately it doesnt work. My problem looks simple but i dont have solution

I have a variable containing date as "2010/04/27" and I want to print all lines from a file where date apprears till the end of file , and I can not hardcode the value of variable since it keeps on changing

any ideas how to achive this ?

will appriciate your help on this

Thanks

Try awk:

awk '$0 ~ dat{p=1}p' dat=$var1 file

You can change the substitution delimiter, not the matching lines one. (edit: methyl proved me wrong on that in post #14)

---------- Post updated at 09:08 ---------- Previous update was at 08:58 ----------

You can use sed to prepend a backslash to each backslash in your variable

var=$(echo $var | sed 's@/@\\/@g')

and then use the sed script I suggested.

also,

var1='2010/04/27'
var2=${var1//\//\\/} # bash shell

sed -n '/'$var2'/,$p'
var="2010/04/27"

You can use ":" delimeter for process like this..

echo $var | sed "s:\/:\\\\/:g"
2010\/04\/27

Why using double quotes when single ones simplify the syntax:

echo $var | sed 's:/:\\/:g'
2010\/04\/27

which isn't that different from what I suggested earlier ("@" vs":")

Somebody hinted at this in an earlier post. Use a different delimiter in sed. We don't need to change the format of the date, we just need to use a different delimiter in sed.

In this case we can use "X" as the delimiter. In the script \X tells sed that this is a different delimiter. The technique is described in "man sed".

Test data  abc04.txt:
line 1
line 2
2010/04/27
line 3
line 4

Script:
#!/bin/ksh
var1='2010/04/27'
cat abc04.txt|sed -n "\X${var1}X,\$ p"


Output:
2010/04/27
line 3
line 4

Thanks for pointing that syntax which I missed in the manual.

The syntax is somewhat obfuscated in the man page.

In the version I have to hand it reads as:

Any fool can understand that. (LOL).
I must confess that I came across the translation some years back in a unix book written in plain English.

So I used to been routine double quotes :o maybe for more than ability :slight_smile: from the others

 
[root@sistem1lnx ~]# echo "This is Test" | sed "s/`echo Test`//g"
This is
[root@sistem1lnx ~]# echo "This is Test" | sed 's/`echo Test`//g'
This is Test

Single quotes prevent the shell from intrepreting backquotes like this :wink:

For second issue I suppose there is no difference between @%,;: :b: