LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 04-20-2006, 04:58 PM   #1
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 492

Rep: Reputation: 75
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.
 
Old 04-20-2006, 04:59 PM   #2
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 492

Original Poster
Rep: Reputation: 75
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'}/
 
Old 04-20-2006, 05:38 PM   #3
ataraxia
Member
 
Registered: Apr 2006
Location: Pittsburgh
Distribution: Debian Sid AMD64
Posts: 296

Rep: Reputation: 30
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')/
 
Old 04-20-2006, 06:39 PM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
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.
 
Old 04-21-2006, 05:14 PM   #5
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 492

Original Poster
Rep: Reputation: 75
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.
 
Old 04-21-2006, 05:56 PM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
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')'/'
 
Old 04-21-2006, 06:26 PM   #7
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,203

Rep: Reputation: 292Reputation: 292Reputation: 292
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.
 
Old 04-21-2006, 06:37 PM   #8
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
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.
 
Old 04-21-2006, 06:42 PM   #9
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 492

Original Poster
Rep: Reputation: 75
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.
 
Old 04-21-2006, 07:26 PM   #10
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,203

Rep: Reputation: 292Reputation: 292Reputation: 292
But you must have some shell? Maybe just sh even?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
script execution - file/directory permission issue serksimper Linux - Enterprise 1 08-10-2005 03:01 PM
Help with a script to edit text file (awk? sed?) rickh Linux - Newbie 8 04-21-2005 08:24 PM
Sed command in file not working lbauer Programming 5 04-06-2005 12:31 PM
how to have no interpretation of special characters (ctrl, tab ...) in a script xround Linux - General 1 11-29-2004 07:21 AM
need script expert for file command illtbagu Linux - General 8 01-01-2004 09:57 PM


All times are GMT -5. The time now is 02:48 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration