You are not logged in.

#1 2008-11-06 17:50:13

thisperishedmin
Member
Registered: 2008-11-04
Posts: 164

sed special character parsing

So I wrote a script for a task today, and it works as expected...but I recycled some code I found floating around on the internet...while I've got a fundamental idea of what its doing, I could use some clarification.  This is my adaptation as based off a somewhat more confusing online example:

DIR=`pwd`
result=$(echo "$DIR"|sed 's!\([]\*\$\/&[]\)!\\\1!g')
echo $result

The first and third line make perfect sense, but on the actual sed command line to parse special characters brings me several points of confusion.

1) result=$(blahblahblah)  -  why is the $ sign following the = ?  I cannot logically understand why this works, but removing it broke the process.  Is it simply passing $result a new variable generated on the fly via the pipe to sed?  If so, why wouldnt it be an additional pipe in the process?

2)  in the sed command, i understand s is substition, and g is for each instance ("global replacement").  but what is the ! for?  Is that simply the delimiter in this particular case?  Does ! have some other crazy meaning in sed? (couldnt find ANYTHING about it in my digging around...)

3) exactly what are the double brackets ( [] ) doing?

Otherwise...it all makes perfect sense tongue

but seriously, if anyone can provide some explanation and expertise I'd love to fully grasp whats going on here!

Thanks in advance!

Offline

#2 2008-11-06 18:18:12

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,390
Website

Re: sed special character parsing

foo=$(bar)

says run bar and assign its output to foo.  In you case, it does a sed on the output of DIR and assigns it to result.

The "!" in the sed statement is the separator.  You can use any character of your choosing, but it is best to stick with something not used in the variables.

Eg:

sed "s!a!b!g"

replaces all instances of "a" with "b". Not the ! separating these.

sed "s#a#b#g"

does that same thing.

I would need to look up #3.  Search for the sed one liner cheatsheet.  It is very useful.

Offline

#3 2008-11-06 18:25:16

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: sed special character parsing

1. $( ) is like ` `
So it captures the output. i.e. line 1 could have been DIR=$(pwd)

2. ! is the / you expect. It can actually be any character you want
e.g. s#a#b#

3. I didn't know this, but apparently to match both [ and ] in the same [...] block you do it like this. (you can't do it with escaping it or putting it in another way)

BTW $PWD is already assigned by default, and there's no need to use \( ... \) if you're not matching anything else, because you can use & in the second field

Offline

#4 2008-11-06 18:58:34

thisperishedmin
Member
Registered: 2008-11-04
Posts: 164

Re: sed special character parsing

wow, thanks for the quick replies guys!  I did not know $PWD was automatically assigned some I'm sure that will come in convenient down the line.

very nice with #1 point too, I didnt realize there was an alternative way of putting this.

2, I thought it may be the case - but I did not realize it could be ANY symbol, again - good to know.

on the 3rd point, that seems like an odd inconsistency but easy enough to keep in mind.

Thank you again for the quick replies!

Offline

Board footer

Powered by FluxBB