Remove leading "#" from a String

Hi Everyone, i am new to scripting and stuck in something.
Query is : I have a properties file eg. data.properties.
This data.properties have some values
example :

key1=value1
key2=value2
#key3=value3

as key3 is commented by #.
so i want to write a script , that will remove # from properties file and if i want to add # infront of any value in properties file.
My script should be able to remove # or add # from a particular string.

Thanks in Advance

Hello Raj87ng,

Welcome to forums, please use code tags as per forum rules for commands/codes/Inputs you are showing into your posts. Following may help you in your requirement.
To remove leading # from the Input_file:

awk '{gsub(/^#/,X,$0);print}' Input_file

To add # into starting of a string.

awk '{gsub(/^key3/,"#"&,$0);print}' Input_file

I haven't tested both, please do let me know if this helps you.

Thanks,
R. Singh

Remove:

awk -F= -v key="key3" '$1=="#"key{$1=key}1' OFS='\=' file

Add:

awk -F= -v key="key3" '$1==key{$1="#"key}1' OFS='\='file

sed versions:

sed 's/^#\(key3=\)/\1/' file
sed 's/^key3=/#&/' file

Some sed version have a -i option which changes the file in-place..

With these two versions, using Perl, you could choose either key or value as the criteria.

The key or value goes inside the first set of /.../
Remove #:

perl -pe  '/value3/ and s/^#//' raj87ng.file
key1=value1
key2=value2
key3=value3

Prefix #:

perl -pe '/value2/ and s/^/#/' raj87ng.file
key1=value1
#key2=value2
#key3=value3