

look ahead and see if another exists, if so remove.So what would be the process in English, well: the actual “programming” is the very last step and if the “English” is logical the programming is generally achieved within a couple of tries. This particular question is one that a beginner should be able to figure out, but I will at least help out a bit.įor most questions I usually start of by writing (or thinking) in English what my steps would be. Looking back over your other posts I see you have asked quite a few questions and have been requested to start helping yourself by reading up on regular expressions. To do so that would require a feature request, which would need to be placed on github, more details on how to do so are here, in our FAQ section.īut to get back to the question in hand.


Your request is not an option using the built-ins, perhaps it could be. Well, you are right to ask as the “built-in” option will indeed do extraneous blanks and EOLs all in one swipe. If not that (gulp), then dare I ask for someone to toss me a regex line? With it, you could have written $ sed -E 's/+$/replace/' file.Said in is there a REMOVE UNNECESSARY BLANK only (not EOL also)?: I did not use the -E switch here because it's not needed. So, your sed command line will thus be $ sed 's/\$/replace/' file.txt However, since *$ also matches nothing (for example, the empty string present at the end of every line), you need to force the matching of something, e.g. will force it to match at the end of the line and nowhere else. If you want to replace anything matching the pattern * at the end of the line with something, then anchoring the pattern like this: *$ Anchoring an expression at the start/end of a line forces it to match exactly there, and not just anywhere on the line. Regular expressions can be anchored at the end of the line using $ (or at the beginning, using ^). In other words, sed reads newline-delimited data, and the delimiters are not part of what a sed script sees. This is because sed reads line by line, and there is therefore no newline at the end of the text of the current line in sed's pattern space.

With standard sed, you will never see a newline in the text read from a file.
