Hello,
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
http://docstore.mik.ua/orelly/unix/sedawk/ch02_03.htm
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 VA 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'
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.
Is there any good website where I can learn sed and regular expressions.
On Mon, Mar 28, 2011 at 8:39 PM, Rony gnulinuxist@gmail.com wrote:
Hello,
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
http://docstore.mik.ua/orelly/unix/sedawk/ch02_03.htm
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 VA 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 FirstOccuranceOnLine-to-LastOccuranceOnLine.
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 line!
Regards R. K. Rajeev
Is there any good website where I can learn sed and regular expressions.
--
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 members. 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 new message.
Regards,
Rony.
2011/3/28 Rony gnulinuxist@gmail.com:
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'
You need to put each substitution in a new line.
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.
The global in "their" context is that "applies to all lines in the file". The "globally" in your /g context is that "applies to all occurrences of pattern in a line". See the difference?
Is there any good website where I can learn sed and regular expressions.
I have a copy of the book you are using; I doubt if there is a better beginner's reference to sed/awk.
Binand
On Monday 28 March 2011 08:39 PM, Rony wrote:
Hello,
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
http://docstore.mik.ua/orelly/unix/sedawk/ch02_03.htm
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 VA Amy Wilde, 334 Bayshore Pkwy, Mountain View CA Sal Carpenter, 73 6th Street, Boston MA
On the site this was a long single line. I broke it into each line for each name and now he substitution happens for every line.
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'
The lines were put one below the other and now it works.
Thanks Rajeev and Binand.