Mëa Cúlpa | 17 Oct 06:23
Picon
Gravatar

Re: Easier way to toggle comments?

Todd A. Jacobs <nospam@...>:
>  I'm using the following to toggle lines on and off in my Ruby .gemrc
>  file:
> 
>      toggle-rdoc () {
>          local FILE="$HOME/.gemrc"
>          { rm $FILE &&
>            awk '/^#/ {sub(/^#/, "", $0); print; next};
>                    /^[^#]/ {sub(/^/, "#", $0); print}' > $FILE
>          } < $FILE
>          cat $FILE
>      }
> 
>  It works, but it just seems like there ought to be an easier way. I
>  couldn't get it to work with sed for some reason; it seems to need
>  some serious branching to work at all in sed. Here's what I tried in
>  sed:
> 
>      alias toggle-rdoc='sed -ri "/^#/{ s/^#// }; s/^([^#])/#\1/"
>  ~/.gemrc; cat ~/.gemrc'
> 
>  but the results are always the same; no toggling happens at all.
>  *sigh*
> 
>  Can anyone think of a more elegant solution for this problem?
> 

Why not use printf instead of sub?

awk ' /^[^#]/ {printf("#%s\n", $0)}'
(Continue reading)

Stephane CHAZELAS | 17 Oct 10:49
Picon
Favicon

Re: Easier way to toggle comments?

2011-10-17, 04:23(+00), Mëa Cúlpa:
> Todd A. Jacobs <nospam@...>:
>>  I'm using the following to toggle lines on and off in my Ruby .gemrc
>>  file:
>> 
>>      toggle-rdoc () {
>>          local FILE="$HOME/.gemrc"
>>          { rm $FILE &&
>>            awk '/^#/ {sub(/^#/, "", $0); print; next};
>>                    /^[^#]/ {sub(/^/, "#", $0); print}' > $FILE
>>          } < $FILE
>>          cat $FILE
>>      }
>> 
>>  It works, but it just seems like there ought to be an easier way. I
>>  couldn't get it to work with sed for some reason; it seems to need
>>  some serious branching to work at all in sed. Here's what I tried in
>>  sed:
>> 
>>      alias toggle-rdoc='sed -ri "/^#/{ s/^#// }; s/^([^#])/#\1/"
>>  ~/.gemrc; cat ~/.gemrc'
>> 
>>  but the results are always the same; no toggling happens at all.
>>  *sigh*
>> 
>>  Can anyone think of a more elegant solution for this problem?
>> 
>
> Why not use printf instead of sub?
>
(Continue reading)


Gmane