LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 02-01-2013, 12:58 PM   #1
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,184

Rep: Reputation: 60
SED special characters issues


I am dusting off the sed cobwebs and had a basic question:

I have a file that contains:
Code:
$firewall = "on";
$cache = "on";
$dataset{'mary had a little lamb'} = "on";
and want to only change the contents of what is between the single quotes:
Code:
$dataset{'big bad wolf'} = "on";
I tried several combinations but to no evail:

Code:
sed 's/^\$dataset{'*'} = "on"\;/\$dataset{'big bad wolf'} = "on"\;/' test.txt
sed "s@^$dataset{'*'} = "on";@$dataset{'big bad wolf'} = "on";@" test.txt
sed 's@^*$@&'big bad wolf' test.txt
So essentially saying, whatever sed finds whats in between the single quotes replace it with "big bad wolf"

Someone please enlighten me!

Last edited by metallica1973; 02-01-2013 at 01:18 PM.
 
Old 02-01-2013, 01:09 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,846

Rep: Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008
This is really more of a shell quoting question. You can use soft quotes to escape hard quotes, and vice-versa. In this case surrounding the whole expression in double-quotes lets you use the single quotes literally.

Code:
sed "/dataset/ s/'[^']*'/'new text'/" infile
 
Old 02-01-2013, 01:28 PM   #3
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,184

Original Poster
Rep: Reputation: 60
Many thanks for the reply. Can you break down your example for clarification? I am trying to make sense of it.

Code:
sed "/dataset/ s/'[^']*'/'new text'/" infile
The way I interpret this is, look for the first occurrence of dataset and look for the first occurrence of the single quotes '' and replace it with whatever text you want to use. I dont understand why you dont have to include "$" in which all the variable in the file have them.

Code:
$dataset{'mary had a little lamb'} = "on";

Last edited by metallica1973; 02-01-2013 at 01:50 PM.
 
Old 02-01-2013, 02:35 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,846

Rep: Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008
Well, it does rely on the assumption that there is exactly one pair of single quotes on the line. Other than that, remember that regular expressions are unbounded, and will match anywhere in the line. You usually don't need to worry about anchoring them to the ends with "^,$" unless the relative position to them is important.

So the address bracket only needs to contain a pattern that matches the line uniquely. If "dataset" isn't enough, then expand it as appropriate.

Similarly, once the line is matched, we're only interested in the quoted part of it, so we can ignore everything outside of them. Regexes match from left to right, so it starts by scanning the line until it matches the first single quote.

Then the next part of the regex is "[^']*", a continuous string of "not a quote" characters. It's necessary to use a pattern like this since regex is greedy. A simple ".*" would keep matching characters all the way to the end of the line.

We finish off the pattern by matching the closing quote.

Finally, since the left-hand-side match "consumed" the quotemarks as well as the string, the right-hand side substitution has to replace them.

We could get fancier and use capturing parentheses and backreferences, to carry values over from the LHS to the RHS, but that's not really necessary here, is it?
 
1 members found this post helpful.
  


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] Passing variables which contain special characters to sed Tech109 Programming 7 06-22-2012 11:08 AM
How to use sed to find lines with special characters bryanwdly Linux - General 2 02-07-2012 12:54 PM
Using sed to replace special characters in a file wskibum Linux - Software 2 03-24-2011 09:47 PM
Help w/ sed parsing special characters clem_c_rock General 8 08-31-2007 04:06 PM
using sed to insert lines with special characters disorderly Linux - Software 26 04-20-2006 05:30 PM

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

All times are GMT -5. The time now is 11:56 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration