Operating System - HP-UX
1833782 Members
2615 Online
110063 Solutions
New Discussion

Re: sed question, tricky one

 
Mike_Ca Li
Regular Advisor

sed question, tricky one

I am trying to get from a general sed command to get the first line up to the line containing a regexp like below:
sed -e '1,/${variable}/!d'
but it does not satisfy the condition when the ${variable} is part of the first line.
Could someone please comment and recommend a fix. Thank you.
9 REPLIES 9
Rodney Hills
Honored Contributor

Re: sed question, tricky one

You could do the following with perl-

perl -n -e 'print if 1../abc/' yourfile

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: sed question, tricky one

Oops, here it is with ${variable}-

perl -n -e "print if 1../${variable}/" yourfile

-- Rod Hills
There be dragons...
Mike_Ca Li
Regular Advisor

Re: sed question, tricky one

Thanks for reply. I guess that it can't be done with sed?
A. Clay Stephenson
Acclaimed Contributor

Re: sed question, tricky one

One thing needs to be made very clear. Are you looking explicitly for ${variable} or aou looking for the instantiation of ${variable}? Note that instantiation will not take place within single quotes but will within double quotes so that your problem may be a shell rather than sed problem.
If it ain't broke, I can fix that.
Mike_Ca Li
Regular Advisor

Re: sed question, tricky one

Hi:
There was a typo, the sed line should be:
sed -e '1,/'${variable}'/!d' filename
or
sed -e '1,/"${ckpoint}"/!d'
But here is what I don't understnad. The content of filename:
---start---
2005011211.zip
2004201004.zip
2005012004.zip
2005011950.zip
2005013102.zip
---end---

sed -e 1,/2005011211.zip/!d filename will return all the 5 lines which is rather strange.
sed -e /2005013102.zip/!d will only return
2004201004.zip

The perl -n -e 'print if 1../2005011211.zip/' will return 2005011211.zip

Is that a sed issue? Thanks


Muthukumar_5
Honored Contributor

Re: sed question, tricky one

You can try as,

------test.log---------
2005011211.zip
2004201004.zip
2005012004.zip
2005011950.zip
2005013102.zip

Execution to print from 1st line to 2005011950.zip as,

# sed -n '{1,/2005011950.zip/p;}q' test.log

It will do it.

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: sed question, tricky one

You can also try as,

# sed -e '{1,/2005012004.zip/!d;}q' test.log for printing from 1 to 2005012004.zip pattern.

hth.
Easy to suggest when don't know about the problem!
Mike_Ca Li
Regular Advisor

Re: sed question, tricky one

$ sed -e '{1,/2005012004.zip/!d;}q' test.log
sed: -e expression #1, char 24: Extra characters after command

$ sed -e '{1,/2005012004.zip/!d;}' test.log
2005011211.zip
2004201004.zip
2005012004.zip

$ sed -e '{1,/200501121.zip/!d;}' test.log
2005011211.zip
2004201004.zip
2005012004.zip
2005011950.zip
2005013102.zip

That was not I was looking for. Thanks for trying. I'll go with the perl solution
vinod_25
Valued Contributor

Re: sed question, tricky one

hi mike

here is the portion of a output from man sed

Same as above but use shell or environment variables var1 and var2 in
search and replacement strings:


sed "s/$var1/$var2/" file1 >file1.out

or

sed 's/'$var1'/'$var2'/' file1 >file1.out

Multiple substitutions in a single command:

hope it sprays some light to your query...

Regards

Vinod K