First a minor point: using g
is useless if you are anchoring the regex with ^
since there can only ever be one match. Now, for the main issue, none of the commands you show would ever match anything since they all have a $
in the left hand side. The $
means "end of the line" so nothing will be matched after that. In addition, you're using [ \t]
which means one of space or tab. What you want is "0 or more" (specifically, you want one or more but 0 or more should work here), so you wanted [ \t]*
. These should do what you want:
sed 's#^//[ \t]title#title#' file.c
sed 's#^//[ \t](#(#' file.c
sed 's#^//[ \t]*"line"#"line"#' file.c
sed 's#^//[ \t])#)#' file.c
But this isn't a good idea since that would replace all cases of commented (
or )
if they are the first non-space or tab character on the line.
However, if you can be sure that you always want to uncomment the line // title
and the three lines after that, you could do:
perl -pe 'if(m#^\s*//\s*title#){ $k=1 }
$k=0 if $k==3;
s#^\s*//## if $k' file.c
On your input file, that produces:
$ perl -pe 'if(m#^\s*//\s*title#){ $k=1 }
$k=0 if $k==3;
s#^\s*//## if $k' file.c
title
(
"line"
)
SomeOtherTitle
(
"otherLine"
)
sed
you have and what other tools might be available. Also, can there be other cases of// (
or// )
in the file which you do not want to uncomment or should every single line with a commented(
or)
as the first non-whitespace character be uncommented?title
and uncomment the next two lines. That would also work