What if you wanted to find and replace an instance or all instances of a string within a file from the command line? “Why would I want to do that?”, you may ask – because we’re too lazy to open a text/code editor and because it’s kewl! You would use a command called sed
(StreamEDitor) thusly:
|
|
i
– edit in place/save changes back to the files
– substitute/find/replace/g
– regular expression matching with the global flag
Example:
|
|
Using extended regex matching
You can use the -r
flag when you want to use extended regexs. So in order to match and remove whitespace you could use the following:
|
|
And you’re off to the races :)