comment text in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comment text in a file
# 1  
Old 04-11-2009
comment text in a file

Hello folks


Hope all are fine, I have query need suggestion, if these lines two lines are already commeted no need to do anything, one more thing order of alpha, gama may be different.


I have a two lines in a file data.txt


%checksum

alpha gama beta penta hexa

I want to do comment both lines so, files should looks like

#%checksum

# alpha gama beta penta hexa



waiting for nice suggestions.

Thanks,
Bash
# 2  
Old 04-11-2009
Hi,
try this:
awk '{print "#"$0}' data.txt > file1.txt

in file1.txt you'll find your commented lines

Bye
# 3  
Old 04-11-2009
Quote:
Originally Posted by S_A_U_R_O_N
Hi,
try this:
awk '{print "#"$0}' data.txt > file1.txt

in file1.txt you'll find your commented lines

Bye

Thank you for prompt reply but there are other lines also, if i use above command it will comment complete file.
# 4  
Old 04-11-2009
try

sed -e 's/%checksum/#&/g' -e 's/alpha gama beta penta hexa/#&/g' file1.txt
# 5  
Old 04-11-2009
Quote:
Originally Posted by zhyl
try

sed -e 's/%checksum/#&/g' -e 's/alpha gama beta penta hexa/#&/g' file1.txt

Thanks, its working but its easy if you can suggest whenever i found "%checksum" comment that line and its next line, it means when found work "%checksum" comment that line and comment its next relevant line.so in that way i can easily do because as i said earlier "alpha gama order can be different".
# 6  
Old 04-11-2009
Ok if i understand, you must match %checksum and comment it then the next line must be commented too?
You may try this:

awk '/%checksum/ {
print "#"$0;getline;
if ( match($0,"") == 1)
{print;getline}
else
getline;print "#"$0;exit }' data.txt > file1.txt

Hope it helps
bye bye
# 7  
Old 04-11-2009
Code:
sed -e '/%checksum/s/^/#/' -e '/[agbp][lae][pmtnx][hat]/s/^/#/' test1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding a comment in a file next to data

Dear Gurus, I have a file which comes every day with set of data, as a part of processing i want to add a comment at the start of every line. e.g of file <PCL> 2E;"HCA";"COP Car A";"ODBS_CFG" 7C;"DD";"Doors Car D";"ODBS_CFG" 3D;"XA";"Auxiliary Car A";"ODBS_CFG" 3E;"XB";"Auxiliary... (12 Replies)
Discussion started by: guddu_12
12 Replies

2. Shell Programming and Scripting

Search and comment block of text from apache httpd.conf

I want to search for a block of text in httpd.conf that between two strings and comment it. There are multiple blocks with "<Directory.. and </Directory>" <Directory "${ORACLE_INSTANCE}/config/${COMPONENT_TYPE}/${COMPONENT_NAME}/htdocs"> # # Possible values for the Options directive are... (3 Replies)
Discussion started by: kchinnam
3 Replies

3. Shell Programming and Scripting

How to comment a specific line of a file?

Hi, I need to comment out (insert # in the front of a line) a line that has entry Defaults requiretty using command-line as I need to do this on hundreds of servers. From Defaults requiretty To #Defaults requiretty I tried something like below but no luck: Please advise,... (3 Replies)
Discussion started by: prvnrk
3 Replies

4. Shell Programming and Scripting

Comment ( -- ) lines from 10 to 25 for every .sql file

Platform : RHEL 5.4 I have several .sql files in a directory. I want to comment lines 10 to 25 for all .sql files. How can I do this ? The symbol for comment in SQL is -- eg: -- select salary from emp where empname = 'URS' ; (3 Replies)
Discussion started by: omega3
3 Replies

5. UNIX for Dummies Questions & Answers

Comment lines in file without vi editor

Legends, Can you please help me in following. I need to comment lines from “/tmp/a.txt” from the line A to line B through the command prompt only. Please use variables not direct values like 2 or 5 It can be done with VI editor but it's not matches with my requirement (: 2,5 s/^/#/g). ... (1 Reply)
Discussion started by: sdosanjh
1 Replies

6. Shell Programming and Scripting

Create a file with comment in script

Hello, Be indulgent for my english. Can you help me ? function f1 { } egrep -v '^#' list_file \ | while read arg1 arg2 arg3 arg4; do f1 $arg1 $arg2 $arg3 $arg4 done In list_file there is I want to replace list_file by a $var then when i launch the script with a file's... (13 Replies)
Discussion started by: amazigh42
13 Replies

7. Shell Programming and Scripting

Comment entries in the file

Legends, Please help me out to come out of below situation. I have a file ABC.txt with the following entries. (example below. actual entries are more than 200 lines) PM3_fun PM4_fun FIMr_mrg ... (5 Replies)
Discussion started by: sdosanjh
5 Replies

8. Shell Programming and Scripting

sudo - prompt for comment/text

Hi. Is there any way to make sudo always prompt for a comment (requirement) before proceding with the actions? (4 Replies)
Discussion started by: th1amigo
4 Replies

9. UNIX for Dummies Questions & Answers

how to replace this text with comment

test compare shown] Replace this text with #test compare shown] (1 Reply)
Discussion started by: manoj.b
1 Replies

10. Shell Programming and Scripting

Need to add a comment line in a text file

Hi I need to add a comment line at the begining of a text file. The scenario is given below. 1. The number of servers that needs to be updated is around 80 2. The location of the text file in all the servers are the same including the file name. 3. The comment has to be added at the very... (2 Replies)
Discussion started by: orakhan
2 Replies
Login or Register to Ask a Question