Appending # to the start of specific line in a properties file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Appending # to the start of specific line in a properties file
# 1  
Old 09-15-2011
Error Appending # to the start of specific line in a properties file

Hi,
I have the following file,

ABC.txt:
Code:
ABC=123
DEF=234
FGH=345

Based on my validation and conditional processing it is observed that i need to comment or append # before DEF=234

so the same file ABC.txt should look as follows
Code:
ABC=123
#DEF=234
FGH=345

Sorry if its a repost/wrong section... this is my first post.
Please help me out here...

Thanks in advance,
Mihir

Moderator's Comments:
Mod Comment Please use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks. See also PM.

Last edited by zaxxon; 09-15-2011 at 08:42 AM.. Reason: code tags, see pm
# 2  
Old 09-15-2011
Everything is fine, just use code tags when you post code, data or logs etc. thanks Smilie

If "DEF=234" is the only identifier to match, you can try:
Code:
sed 's/^DEF=234/#&/' infile

# 3  
Old 09-15-2011
Error Appending # to the start of specific line in a properties file

I dont want starts with, i want it to be something like,
I want to search 234 value and append # to the same line which contains the string 234

for instance
Code:
ABC=123
DEF=234
EFG=2356
DFGT=234

should be updated to
Code:
ABC=123
#DEF=234
EFG=2356
#DFGT=234

Can you help me out on this?

Sorry for the confusion earlier... It seems i failed to phrase the proper query.
# 4  
Old 09-15-2011
Code:
$ nawk '{if ($0~/234/) {print "#"$0}else {print $0}}' infile

# 5  
Old 09-15-2011
No worries, I think I missed that point^^
Code:
sed 's/.*234.*/#&/' infile
ABC=123
#DEF=234
EFG=2356
#DFGT=234

Edit:
And the awk line a tad shorter:
Code:
awk '/234/ {print "#"$0; next}1' infile

# 6  
Old 09-15-2011
Code:
 
$ perl -lane 'if($_ =~ /234/){print "#$_";}else{print $_}' test
ABC=123
#DEF=234
EFG=2356
#DFGT=234

# 7  
Old 09-16-2011
Code:
sed '/234/s/^/#/' infile

Code:
awk '/234/{$0="#"$0}1' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Appending content of a file to another file before a specific character

Hi there, i've got a file with this content $ cat file1 Matt Mar The other file has the same number of lines with this content: $ cat file2 20404=767294 23450=32427 is there a way with either using sed, awk or paste to insert the content of file1 before the "=" character? So... (3 Replies)
Discussion started by: nms
3 Replies

2. Shell Programming and Scripting

Bash to goto specific line/function and start processing if user response is yes

In the bash below I am trying to run the script entire script including the ....(which is a bunch of code) and then in the run function if the user response is y (line in bold). then start processing from execute function. Basically, goto the # extract folder for variable filename line and start... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. UNIX for Advanced & Expert Users

Appending a files contents to the end of a specific file name in several directories

Here is my dir structure: /tmp/dave/myappend.txt /tmp/dave/dir1/test.txt /tmp/dave/dir2/test.txt /tmp/dave/dir3/test.txt /tmp/dave/dir4/test.txt I want to append the contents of myappend.txt to the end of each file with the name "test.txt" in all dirs in /tmp/dave/ I have tried this:... (2 Replies)
Discussion started by: bigd213
2 Replies

4. Shell Programming and Scripting

Bash - Appending to specific line in file

I'm working on a personal project, a multiplication quiz script for my kids. In it, the user's performance will be recorded and written to a file. After they've played it a little while, it will start to focus more on the ones that give them the most trouble-- that take a long time to answer or... (4 Replies)
Discussion started by: treesloth
4 Replies

5. UNIX for Dummies Questions & Answers

Appending a character(#) with string search at the start of the line

Hello, I have been browsing through the forum, but unable to find a solution for my requirement. I need to go through a file and search for /home/users and insert a # symbol at the start /home/users. Example output is #/home/users. Can you please help me with the awk or sed command for... (1 Reply)
Discussion started by: chandu123
1 Replies

6. Shell Programming and Scripting

Put a # in start of a specific line of a file

Hello Guys Please let me know how to solve the below issue I have a file like below drop table R1416.ABC1 cascade constraints; drop table R1416.ABC2 cascade constraints; drop table R1416.ABC3 cascade constraints; drop table R1416.ABC4 cascade constraints; drop table R1416.ABC5... (7 Replies)
Discussion started by: Pratik4891
7 Replies

7. UNIX for Dummies Questions & Answers

.properties file and new line feeds

Hi, I have a .properties file that a read in some values in an .sh file but everytime I put it out on the server it fails. If I copy and paste the values of the .properties file on my local machine to the .properties file on the server it works just fine. Someone mentioned to see if it has dos... (3 Replies)
Discussion started by: vsekvsek
3 Replies

8. Shell Programming and Scripting

Appending the line number and a seperator to each line of a file ?

Hi, I am a newb as far as shell scripting and SED goes so bear with me on this one. I want to basically append to each line in a file a delimiter character and the line's line number e.g Change the file from :- aaaaaa bbbbbb cccccc to:- aaaaaa;1 bbbbbb;2 cccccc;3 I have worked... (4 Replies)
Discussion started by: pjcwhite
4 Replies

9. Shell Programming and Scripting

Reading specific contents from a file and appending it to another file

Hi, I need to write a shell script (ksh) to read contents starting at a specific location from one file and append the contents at specific location in another file. Please find below the contents of the source file that I need to read the contents from, File 1 -----# more... (5 Replies)
Discussion started by: dnicky
5 Replies
Login or Register to Ask a Question