Project

General

Profile

Feature #394

Comment And Uncomment All Lines in a Linux File with Sed

Added by Daniel Curtis almost 10 years ago. Updated almost 10 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
-
Target version:
-
Start date:
05/21/2014
Due date:
% Done:

100%

Estimated time:
0.10 h
Spent time:

Description

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). 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:
  1. 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
  2. sed requires escaping of capturing parentheses, hence the \( and \) in the first example
  3. The final ‘g’ at the end of the expressions means “global”, i.e. replace all occurrences
#1

Updated by Daniel Curtis almost 10 years ago

  • Status changed from Resolved to Closed

Also available in: Atom PDF