[SOLVED] sed "expression #1, char 2: extra characters after command"
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
'g', in vim (and ed), is the global command, i.e. run the following command(s) on every line that matches the given regex. sed doesn't have a global command because it always operates sequentially anyway. global operation is the default.
(Actually, sed does have a 'g' command, but it's a different operation, getting text from the hold buffer.)
From things like this, it seems to me that the OP's trouble with sed is that he's expecting its syntax and operation to be almost exactly like vim. But it's not. While there are many similarities due to their shared development history, there are also many differences, due to the way the programs operate.
vim is a full text editor, which means that the entire file is first loaded into a memory buffer, then individual lines are processed, in a random-access way, using address flags. But sed is the "stream editor", meaning that it processes one line at a time, in one direction only, from the first to the last. sed's addressing system is there simply to match lines for processing as they pass by.
Because of this, real multi-line editing in sed, particularly if you need to backtrack and access previous lines, can be tricky to do and usually requires the use of the hold buffer to store text temporarily for later access. But for simple one-line-at-a-time work, it's very easy and efficient.
OK - that's a single line with only one change to make 200 odd times and the regex:
would work fine with it. The g at the end of the line means the expression finds a line that contains the pattern for which it's searching and does whatever it has to for every occurrence of the pattern on that line. Then it stops.
But this next example is different:
2 ~@~\We removed functionality because ~@~Xthat
3 approach is broken,~@~Y for degrees of broken from
4 ~@~Xit is a security hole~@~Y all the way to ~@~Xit does not
5 conform to the new style we are using.~@~Y~@~]
6 This ~@~Xattribution~@~Y obviously didn~@~Yt go down
Here there are many lines, and line 6 has 2 occurences of the pattern. In this case. the g at the start of the line searches every line but only acts on the first incident of the pattern on each line. This would would work on my original example, the pattern searched for occurred only once every two lines but it would fail to work on line 6 (above). The pattern we need here is:
a g at each end means that every line is searched & every incident dealt with. And, bcause I'm using vim, all I do is flip through my history, find a regex that more or less matches my reqirements and edit it. They may have been sweated over to start off with but thyey're all boiler plates now.
Last edited by TheBigMing; 03-20-2013 at 03:54 PM.
@David the H - Aha we've met before. Thanks for your reply, I'll certainly chase up your rfeferences. I know about ex & ed - one or the other forms the basis of the vim 'command line' - but I've never used either in anger. I use awk quite often, which I like, but it would be nice to have something a more lightweight.
I suppose I'll have to bite the bullet & come to terms with sed - whether I will or not's another matter, but thanks for your help.
Re regex differences; in fact I'd never assume any 2 tools have EXACTLY the same regex engines, even though certain symbols definitions are pretty constant.
Try http://regex.info/book.html if you want the full skinny; highly recommended
vim does some funky stuff with newlines, converting them to carriage returns when the text is loaded in a buffer. I had to learn, for example, that the LHS of the s command uses '\n' to match newlines, but the RHS uses '\r' to insert them.
The other programs don't have this behavior, and always use the default '\n'. So in sed you have to use '\n' on the RHS to insert a newline.
Also, sed's pattern buffer will have no newlines to match in it unless you've previously told it to combine multiple lines together (through the N, G, or H commands). Only then can you use '\n' on the LHS to match them. This can sometimes be quite complex to do correctly, another reason why I don't recommend sed for multi-line processing.
@grail: '%' is a simple range address, equal to '1,$', which does do the job much of the time. But sometimes you need to run a command globally while also using a matching pattern. e.g. if you want to delete every line that contains the pattern 'foo' you'd have to use ":g/foo/d".
'g' can also accept a range itself, so you can limit the deletions to the first 20 lines with ':1,20g/foo/d'. Finally, in ed at least, it can be used to run multiple commands at once.
Last edited by David the H.; 03-26-2013 at 12:21 PM.