Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-20-2006, 04:58 PM
|
#1
|
Member
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542
Rep:
|
Command interpretation issue in Sed script file
Hi
Her is the issue
when i type my script on the command line it's working
Code:
$echo "The date of today is: 10/02/05 |sed -e "/date/s/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9]/$(date '+%d\/%m\/%y')/"
$The date of today is: 20/04/06
But when i put this command line in a sed script file, it doesn't take the command but as a string. So i have this kind of output
Code:
$echo "The date of today is: 10/02/05 |sed -f my-sed-script.script
$The date of today is: $(date '%d\/%m\/%y')
here is what i put inside the file "my-sed-script.script"
Code:
#!/bin/sed -f
/date/s/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9]/$(date '%d\/%m\/%y')/
Last edited by angel115; 04-20-2006 at 05:02 PM.
|
|
|
04-20-2006, 04:59 PM
|
#2
|
Member
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542
Original Poster
Rep:
|
I have try the following as well in the file "my-sed-script.script" with out success
Code:
#!/bin/sed -f
/date/s/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9]/${date '%d\/%m\/%y'}/
Code:
#!/bin/sed -f
/date/s/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9]/(date '%d\/%m\/%y')/
Code:
#!/bin/sed -f
/date/s/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9]/{date '%d\/%m\/%y'}/
|
|
|
04-20-2006, 05:38 PM
|
#3
|
Member
Registered: Apr 2006
Location: Pittsburgh
Distribution: Debian Sid AMD64
Posts: 296
Rep:
|
You're missing the + from in front of the date format.
Code:
#!/bin/sed -f
/date/s/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9]/$(date '+%d\/%m\/%y')/
|
|
|
04-20-2006, 06:39 PM
|
#4
|
Member
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557
Rep:
|
I don't think sed can interpret $(...). Your shell is doing that on the command line before sed even sees the script.
In the file, sed sees the $(...) stuff and doesn't do anything special with it.
|
|
|
04-21-2006, 05:14 PM
|
#5
|
Member
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542
Original Poster
Rep:
|
Hi Aluser
Thanks for this clarification, So if i understand well if i define the variable first it should be possible to use the value of this variable in my sed script? But how? because it still take my variable as a string
Code:
#!/bin/sed -f
/date/s/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9]/'$DATEVAR'/g
give me the output:
Code:
$echo "The date of today is: 10/02/05 |sed -f my-sed-script.script
$The date of today is: '$DATEVAR'
Last edited by angel115; 04-21-2006 at 05:16 PM.
|
|
|
04-21-2006, 05:56 PM
|
#6
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
Would you settle for bash?
Code:
#!/bin/bash
echo "The date of today is: 10/02/05" | \
sed -e '/date/s/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9]/'$(date '+%d\/%m\/%y')'/'
|
|
|
04-21-2006, 06:26 PM
|
#7
|
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467
Rep: 
|
Or even:
Code:
#! /bin/bash
echo "Today's date is:" `date "+%d/%m/%y"`
Last edited by dive; 04-21-2006 at 06:28 PM.
|
|
|
04-21-2006, 06:37 PM
|
#8
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
That's too easy! I think it was an exercise in using sed. 
You could cut the sed down abit...
Code:
#!/bin/bash
echo "The date of today is: 10/02/05" | \
sed -e '/date/s/[0-9]*\/[0-9]*\/[0-9]*/'$(date '+%d\/%m\/%y')'/'
or
sed -e '/date/s/..\/..\/../'$(date '+%d\/%m\/%y')'/'
or
sed -e '/date/s/..\/.*/'$(date '+%d\/%m\/%y')'/'
Last edited by homey; 04-21-2006 at 06:41 PM.
|
|
|
04-21-2006, 06:42 PM
|
#9
|
Member
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542
Original Poster
Rep:
|
Hi
Yes, i would do that if i could.... But unfortunately the environement where i'm using it doesn't alow me to execute bash script.
So i absolutly need to put this in a sed script.
I just need to know if it's possible to use a variable in a sed script. and if it's possible how?
Kind regards
Angel.
|
|
|
04-21-2006, 07:26 PM
|
#10
|
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467
Rep: 
|
But you must have some shell? Maybe just sh even?
|
|
|
All times are GMT -5. The time now is 03:57 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|