Remove leading "#" from a String


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove leading "#" from a String
# 1  
Old 05-20-2016
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 :
Code:
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

Last edited by Scrutinizer; 05-20-2016 at 04:47 PM.. Reason: code tags
# 2  
Old 05-20-2016
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:
Code:
awk '{gsub(/^#/,X,$0);print}' Input_file

To add # into starting of a string.
Code:
awk '{gsub(/^key3/,"#"&,$0);print}' Input_file

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

Thanks,
R. Singh
# 3  
Old 05-20-2016
Remove:
Code:
awk -F= -v key="key3" '$1=="#"key{$1=key}1' OFS='\=' file

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


sed versions:
Code:
sed 's/^#\(key3=\)/\1/' file

Code:
sed 's/^key3=/#&/' file

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

Last edited by Scrutinizer; 05-20-2016 at 05:21 PM..
# 4  
Old 05-20-2016
Quote:
Originally Posted by Raj87ng
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 :
Code:
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
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 #:
Code:
perl -pe  '/value3/ and s/^#//' raj87ng.file
key1=value1
key2=value2
key3=value3

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

2. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

3. Shell Programming and Scripting

tcsh - understanding difference between "echo string" and "echo string > /dev/stdout"

I came across and unexpected behavior with redirections in tcsh. I know, csh is not best for redirections, but I'd like to understand what is happening here. I have following script (called out_to_streams.csh): #!/bin/tcsh -f echo Redirected to STDOUT > /dev/stdout echo Redirected to... (2 Replies)
Discussion started by: marcink
2 Replies

4. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

5. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

Remove path string from file (string contains "/")

This is the way sed -i 's/home/$USER/.config/hello_there//g' /home/$USER/.gnomerc But, as far as I saw you cannot add any "/" in the string you want to remove.... So, what should I do in order to remove this path (which contains "/") ?:confused: (7 Replies)
Discussion started by: hakermania
7 Replies

8. Shell Programming and Scripting

input string="3MMTQSZ348GGMZRQWMJM4SD6M";output string="3MMTQ-SZ348-GGMZR-QWMJM-4SD6

input string="3MMTQSZ348GGMZRQWMJM4SD6M" output string="3MMTQ-SZ348-GGMZR-QWMJM-4SD6M" using linux shell script (4 Replies)
Discussion started by: pankajd
4 Replies

9. Shell Programming and Scripting

"read" command ignoring leading spaces

I have to read a file line by line, change it and then update the file. Problem is, when i read the file, "read" command ignores leading spaces. The file is a script which is indented in many places for clarity. How to i make "read" command read leading spaces as well. (3 Replies)
Discussion started by: vickylife
3 Replies

10. Shell Programming and Scripting

How to remove "New line characters" and "spaces" at a time

Dear friends, following is the output of a script from which I want to remove spaces and new-line characters. Example:- Line1 abcdefghijklmnopqrstuvwxyz Line2 mnopqrstuvwxyzabcdefghijkl Line3 opqrstuvwxyzabcdefdefg Here in above example, at every starting line there is a “tab” &... (4 Replies)
Discussion started by: anushree.a
4 Replies
Login or Register to Ask a Question