Posted on 09/02/2010. By Pete Otaqui.
Just a couple of sed one-liners for adding and removing comments in the form of # marks (in the case of my ~/.ssh/config file). I use this to toggle proxy settings (with connect.c) and it’s run by the awesome MarcoPolo location-aware app for OS X. Both of these are safe to run repeatedly (you won’t end up with multiple # marks or anything).
First adding comments, which means a “#” mark at the start of every line
sed -i '' 's/^\([^#]\)/#\1/g' ~/.ssh/config
Second removing the comments, just stripping out the “#” marks.
sed -i '' 's/^#//g' ~/.ssh/config
A few things to note here, on the assumption that you can see how Regular Expressions work and are vaguely familiar with sed’s /find/replace/ style syntax:
- the “-i” flag means “edit in place” and requires an extra argument for the backup file’s extension. I’ve given an empty string so that no backup is made
- sed requires escaping of capturing parentheses, hence the \( and \) in the first example
- the final ‘g’ at the end of the expressions means “global”, i.e. replace all occurences