sneakyimp;11050545 wrote:It's a little confusing trying to sort out what's sed syntax, what's bash syntax, and what's your framework syntax. I appreciate the explanation and examples, though.
Let me see if I can't break one set of the commands to a bit of piecemeal:
sed -e 's#.*LoadStyle.*#<link rel="stylesheet" type="text/css" href="{Config>GetValue(website,site_url)}styles/main-build.css">#' \
-e '/AddStyle/d' -i ${DEPLOY_DIR}app/view/main.html
Can be rewritten as 2 commands, and I'll remove the bash variable for simplicity:
sed -e 's#.*LoadStyle.*#<link rel="stylesheet" type="text/css" href="styles/main-build.css">#' -i /app/view/main.html
sed -e '/AddStyle/d' -i /app/view/main.html
So now its just 2 find and replaces. The first says find a line starting with anything (.) leading up to LoadStyle and with anything (.) to the end of the line. Then it rips everything that matches, and changes it to a normal link tag to load css. The second says find any line containing AddStyle and delete the whole line (the d flag does the delete). Finally -i name_of_file which means do the replacement in place, instead of printing it to stdout.
sneakyimp;11050545 wrote:That sounds like a potent command! However, the ':a;N;$!ba;s/\n//g' syntax is a total mystery to me. The other ones look pretty confusing too. It's apparent that the syntax for sed commands is quite unlike preg in php. If I want to develop any facility with this, I'll obviously need to learn this alternate syntax.
See http://stackoverflow.com/a/1252191 the exact overflow answer I stole it from as there is a good explanation of how it works.
sneakyimp;11050545 wrote:* what characters are permissible as expression delimiters? I've seen / and @ and # -- is there any restrictions on these? Obviously quotes are unacceptable -- or at least ill-advised
I found this, but I don't know of any exhaustive list... looks like just about anything though.
sneakyimp;11050545 wrote: In the event that one must also use the delimiter char in the expression* then how do you escape it? My attempt before in preceding slashes (my delimiter) with a backslash didn't seem to work.
Are you using single quotes to surround your pattern? if not you need to escape the escape, similar to escaping in double quotes in php. {char} becomes an escape sequence in double quotes or without quotes in bash, so you would need to double them up (iirc). Or you can use single quotes, which means pass this literal string without any bash interpretation as an argument to the command.
sneakyimp;11050545 wrote:* what's the best way to get the hang of sed patterns? any good tutorials or quick start guides?
Practice practice practice. Even engineers need to work out to improve their skills I can't tell you how many failed sed commands I've written before I got the ones that worked exactly as I wanted.