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"; Code:
$dataset{'big bad wolf'} = "on"; Code:
sed 's/^\$dataset{'*'} = "on"\;/\$dataset{'big bad wolf'} = "on"\;/' test.txt Someone please enlighten me! |
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 |
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 Code:
$dataset{'mary had a little lamb'} = "on"; |
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? |
All times are GMT -5. The time now is 02:25 PM. |