Google Answers Logo
View Question
 
Q: syntax for sed command ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: syntax for sed command
Category: Computers > Algorithms
Asked by: questioner1234-ga
List Price: $2.00
Posted: 05 Nov 2004 14:09 PST
Expires: 05 Dec 2004 14:09 PST
Question ID: 424983
I have a sed command I need to modify. here it is now:
sed "s/$1//g"

I would like to add some text to the $1 string. I would like something like:
sed "s/'www.',$1,'.com '//g"
but I don't know the correct syntax to do it. So I would like to ad
"www." to the beginning of the string and ".com " to the end of the
string.
Answer  
Subject: Re: syntax for sed command
Answered By: mathtalk-ga on 05 Nov 2004 21:53 PST
Rated:5 out of 5 stars
 
Hi, questioner1234-ga:

The usual sed command for global string search and replace is this:

s/BRE/replacement/g

where BRE stands for a "basic regular expression" and replacment is
used here as a stand-in for whatever actual replacement text is
intended.  The "g" at the end has the effect of making the search and
replace "global", ie. to act on every match of the BRE instead of (by
default) the first instance on each line.

It looks like your sed command is constructed to be used within a
shell script, which is often the case.  That would mean $1 takes its
value from the first command line argument following the name of the
invoked shell script.

Because your replacement string is empty, it has the effect of
deleting every matching instance in the input file.

So putting all these observations together, I will stick my neck out
and guess that the modified command should be:

sed "s/www\\.$1\\.com//g"

I think the idea here is that you want to supply a "domain name" on
the command line, then have the shell script take (standard?) input
and remove every occurance of the URL www."domain name".com before
writing it to (standard?) out.

The backslashes here are "escape characters" that convert the period
into simply a literal period.  Otherwise the period would have the
regular expression meaning of matching any single character (except
NUL).  But an educated guess is that you are looking for the literal
periods as part of the specific URL as your matching pattern.

There are many sites on the Web that can tell you more about sed and
clever tricks it is useful for, but here's a starting point:

[sed -- Unix for Advanced Users]
http://www.uwsg.iu.edu/UAU/advcomm/SED.html

Note in particular the explanation given there about "escaping" the
period in the search pattern (BRE), but this would not be needed in
the replacement string portion.

A fairly lengthy "man page" treatment of sed is here:

[sed -- stream editor]
http://www.opengroup.org/onlinepubs/007908799/xcu/sed.html

with links to discussion of the basic regular expression syntax common
to other tools like awk and grep.

Before closing I'd like to say a few words about the shell scripting
environment and it's relationship to the proposed command, esp. the
use of double quotes.

What we have with a simple line like:

sed "s/$1//g"

in a shell script is a template that the shell program (bash, for
instance) will act on before submitting for execution.

The text inside the double quotes will be massaged by the shell before
the sed program itself ever "sees" it.  There is special treatment
within the double quotes for:

dollar signs ($)
back quotes (`)
backslashes (\)

and even more fundamental, the pair of double quotes "holds together"
the command string s/$1//g as a single "token", ensuring that when the
sed program sees its command line arguments, this command will be
treated as all one item.

As we mentioned before, the dollar sign follow by 1 ($1) will get
substituted out by the shell program, using instead whatever the first
argument was on the command line with the shell script executed that
contains this line.  The shell also uses the backslash character to
"escape" special characters from this kind of substitution, e.g. if
you want to pass in a real dollar sign, say, as part of a command line
argument.  In particular the backslash can be used to escape itself
within double quotes, so that sed will see a single backslash where
we've put \\ in the double quoted string for the shell.  [Then,
finally, sed will apply that single backslash to escaping the period! 
Confusing, hunh?]

I won't go into what back quotes will do; it's a bit bizarre in
comparison to what we've discussed and quite irrelevant to the task at
hand.  But if you'd like to learn more about Unix shells and quoted
strings on the command line, here's a place to start:

[Unix Shell Quote Tutorial by Bruce Barnett]
http://www.grymoire.com/Unix/Quote.html


Give the suggestion above a shot, and please let me know if it doesn't
quite do what you hoped it would!

regards, mathtalk-ga
questioner1234-ga rated this answer:5 out of 5 stars and gave an additional tip of: $2.00

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy