On Mon, Mar 28, 2011 at 8:39 PM, Rony <gnulinuxist(a)gmail.com> wrote:
I am trying to get a grip on sed and regular expressions. I found this
site which looked nice but it appears to have mistakes in its examples.
For eg. on this page
There is a file called 'list' and all commands are tested on this file.
The file contents are:-
John Daggett, 341 King Road, Plymouth MA Alice Ford, 22 East Broadway,
Richmond VA Orville Thomas, 11345 Oak Bridge Road,
Tulsa OK Terry Kalkas, 402 Lans Road, Beaver Falls PA
Eric Adams, 20 Post Road, Sudbury MA Hubert Sims, 328A Brook Road, Roanoke
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA Sal Carpenter, 73 6th
Street, Boston MA
They have created a script file called sedscr. I have put the text as
asked by them which is pasted below
s/ MA/, Massachusetts/ s/ PA/, Pennsylvania/ s/ CA/, California/ s/ VA/,
Virginia/ s/ OK/, Oklahoma/
However when I give this as input to the file called 'list', I get an
error:- sed: file sedscr line 1: unknown option to `s'
Simple one here, you need to give each of the s statements on a new line.
That should fix that for you. Dunno of a Semicolon separator might work too.
In the earlier examples of substitution sed 's/MA/Massachusetts/" list
works only on the first instance of MA and not all which
goes against their statements that sed commands are global by default
unless the address is specified. The above command
will work globally only if the /g option is put in the right had side.
Rony, you appear to be confusing the scope of the term global. In terms of
general command addressing(the kind you put in numeric or match style before
any command), global means FirstLine-to-LastLine. In this respect, since you
have not provided any addressing, it means that the s command will be
executed Once-For-Each-Line in the global scope(i.e., for all lines in
input). But, within each of the individual s(substitution) commands, the
scope is line level, so the option g(global) here refers to the scope
P.S. - The same logic also applies when you use the s command in vi/vim,
with a 1,$ address range(or for that matter, any range of lines).
P.P.S - Also, do verify your file encodings. I've had cases where broken
conversion or interpretation of Line Ending(CR/LF) characters has lead to
scripts seeing an entire input file as containing just one veeeery long
R. K. Rajeev
Is there any good website where I can learn sed and
As a proper list etiquette.....
Please trim your replies. Avoid cross posting to other lists.
Members do not want to waste time answering same queries that may have been
answered on other lists.
Replies from other lists, to cross posted mails are not available to our
Post your replies below the relevant original text, leaving a line space.
Do not re-use old messages to write new ones. For new messages, create a