Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to use sed to comment out a certain command in a js file. The command is called processLoad(..). I came up with expression below but it doesn't do what I expect it to do. The computer that I am on belongs to my provider, so I do not know the version of linux that is running on the machine. [added:]

sed -r -i 's_^\([ \t]+processLoad.*\)$_//\1_' test.txt

I hope to replace a line:

     processLoad('mystr');

into

//     processLoad('mystr')

I figured out that '/' as substitute separation character would pose problems with the // as comments, so I choose _ as substitute separation character.

share|improve this question
1  
Give an example line of input & your desired output. –  jftuga Aug 25 '11 at 19:02
1  
What is it doing? –  soandos Aug 25 '11 at 19:04

4 Answers 4

up vote 2 down vote accepted

Try sed '/^[[:space:]]*processLoad/s!^!//!'


The first part /^[[:space:]]*processLoad/ limits actions to only lines matching that pattern. The second part s!^!//! is sed's famous search-and-replace: Find the beginning of the line (^) and replace with two slashes. I use bangs (s!!!) instead of the typical slashes so I don't have to bother escaping the literal slashes in the replacement string. For more details, see the sed man page.

share|improve this answer
    
Thanks for explaining, I've updated your answer to include that so feel free to delete the comment. –  Tom Wijsman Aug 29 '11 at 15:44

Perhaps you should change the + to * in case processLoad occurs at the start of a line.

If processLoad(...) can occur in the middle of a line you'd need a rather different expression.

share|improve this answer

Do you need the extended regexp flag (-r)?

With this flag, your regexp is trying to match literal ( and ) rather than store the match.

The only difference between basic and extended regular expressions is in the behavior of a few characters: ‘?’, ‘+’, parentheses, and braces (‘{}’). While basic regular expressions require these to be escaped if you want them to behave as special characters, when using extended regular expressions you must escape them if you want them to match a literal character.

Extended regexps link

share|improve this answer
    
The -r flag was because it was not working without the -r flag (it should have been removed) Currently I'm not in the position of verifying but maybe I'm also mixing regexp dialects (emacs, perl, grep) and is the grouping wrong (..) instead of `(..)' –  dr jerry Aug 26 '11 at 12:44

Expression below solved it, for me it works on debian linux, and mac os x (bsd), although the \1 needs to be substituted with $1. Kind of irritating that sed differs so much across different platforms.

`sed -e 's_^\([ \t]*processLoad(\)_//\1_' -i -- test.txt
share|improve this answer
    
if you remove the --, does it do the same? i.e. if it reads -i instead of -i -- –  barlop Aug 29 '11 at 16:02
    
@barlop Difference is in the way sed options are parsed. Mac os x requires a bk up extension with the -i option, whereas the linux version is just happy with -i without an extension. If i would do sed -i 's/a/b/ os x complains with an "undefined label" error. As an alternative I could have done sed -i '' 's/a/b/'. Main objective was to get it work on os x an linux. –  dr jerry Aug 30 '11 at 14:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.