LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-03-2014, 11:33 AM   #1
thealmightyos
Member
 
Registered: Mar 2009
Distribution: CentOS 6.5 / 7
Posts: 119

Rep: Reputation: 1
sed: variable in a search string


High all! Having a little issue with sed and wondering if anyone has some insight

What I am trying to do: find all instances of "00/00/0000" within a file and replace with the current date. This is within a batch file with a variable for current date ($date).

Here is my code:
Code:
sed -i 's/\00\/00\/2000/"${date}"/g' /file.txt
What's happening:
The file is edited. All instances of "00/00/0000" have been replaced with ""${date}"".

What am I missing?
 
Old 04-03-2014, 11:39 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
The double quotes inside single quotes are interpreted literally and the variable expansion is not performed. You can always embed the whole sed command in double quotes (until there is no ambiguity for the shell), e.g.
Code:
sed -i "s/00\/00\/2000/${date}/g" /file.txt
Also take in mind that you can use any character as separator in the s command. This is useful when the regular expression or the replacement string contain slashes:
Code:
sed -i "s:00/00/2000:${date}:g" /file.txt

Last edited by colucix; 04-03-2014 at 11:42 AM.
 
1 members found this post helpful.
Old 04-04-2014, 10:42 AM   #3
thealmightyos
Member
 
Registered: Mar 2009
Distribution: CentOS 6.5 / 7
Posts: 119

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by colucix View Post
The double quotes inside single quotes are interpreted literally and the variable expansion is not performed. You can always embed the whole sed command in double quotes (until there is no ambiguity for the shell), e.g.
Code:
sed -i "s/00\/00\/2000/${date}/g" /file.txt
Also take in mind that you can use any character as separator in the s command. This is useful when the regular expression or the replacement string contain slashes:
Code:
sed -i "s:00/00/2000:${date}:g" /file.txt
That worked! I always mess up where to put the quotes at and if I should use a single or a double. Programmer I am not.

Thanks for the help
 
  


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
[SOLVED] Sed/awk/cut to pull a repeating string out of a longer string StupidNewbie Programming 4 09-13-2018 04:41 AM
[SOLVED] search and replace string with special character perl or sed. Noobux Programming 4 05-21-2012 04:16 PM
[SOLVED] sed: global search and replace if a string isn't anywhere in that line linux_kung_fu Linux - General 5 03-09-2012 11:53 AM
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 12:05 PM
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 02:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:55 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration