Comment entries in the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comment entries in the file
# 1  
Old 06-04-2010
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
FIMr_gap
FIMr_copy
FIMser_sync
FIMer_rep
FIMer_rep

Now i want to comment out these lines (#) in front of these.
How do i do that?

Regards,
dossanjh
# 2  
Old 06-05-2010
If you do want to specify different line patterns, you can do something like:
Code:
sed -e 's/\(^PM3_fun$\)/#\1/g' -e 's/\(^PM4_fun$\)/#\1/g' ABC.txt

This will comment lines "PM3_fun" and "PM4_fun".
add more -e parts in the same way for other lines.

Also. PM3 and PM4 can be combined as PM[34], and in the same way for other patterns also.
But if you having only these lines, specifying explicitly won't be harder too.
# 3  
Old 06-05-2010
Anchal,

Thanks for the reply, but i have near about 200 different patterns.
# 4  
Old 06-05-2010
one question, ABC.txt has around 200 lines with different pattern along with other lines which is not to be commented?

if this is the case, you have to provide the identified key for how to determined the line which is to be commented.

other way, if you having all the patterns in a separate file, then probably it would be easier.
# 5  
Old 06-05-2010
ABC.txt has 2400 lines in total and out of that 200 line of some pattern that needs to be commented. rest of the lines should be untouched.

For the lines to be commented pattern, i have got those in a temp file to use it as an identified key.

---------- Post updated at 08:40 PM ---------- Previous update was at 08:36 PM ----------

I am facing hard luck in the previous solution as well. The command runs but not commenting enteries.

sed -e 's/\(^FIM$\)/#\1/g' /tmp/abc


san% grep FIM /tmp/abc
FIM1
FIM2
FIMR45

---------- Post updated at 08:54 PM ---------- Previous update was at 08:40 PM ----------

Got the quick workaround to enjoy my weekend Smilie


perl -p -i -e "s/PM/#PM/g" ABC.TXT

Last edited by sdosanjh; 06-07-2010 at 02:43 AM..
# 6  
Old 06-05-2010
If you have a file with the keys like:
Code:
PM3
PM4
FIMr
.
.

you can do something like:
Code:
awk -F_ '
NR==FNR{a[$0];next}
$1 in a {$0 = "#" $0}
{print}' keyfile file

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

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

3. Shell Programming and Scripting

To comment/uncomment in config file

hi! I want to comment and uncomment 2 lines in my config files that goes like: CONTACT_LIST="abc@xyz.com;" #CONTACT_LIST="def@xyz.com;" I want to sawp the commnets in above lines and desired output should be: #CONTACT_LIST="abc@xyz.com;" CONTACT_LIST="def@xyz.com;" Please suggest. (1 Reply)
Discussion started by: scriptNovice
1 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

how to find and comment out lines in one file against another file

I have 2 files: fileA and fileB. content of fileA --------------- admin.teacher is in new york; admin.mason is in new york; admin.driver is in new york city; user.trucker is in hartford; admin.developer is in new york state; content of fileB ---------------- admin.teacher is in... (2 Replies)
Discussion started by: lowprofile
2 Replies

8. Red Hat

Comment in static route file

Hi all, is it possible to add a comment starting with "#" (with vi for example) to the static route file in: /etc/sysconfig/network-scripts/route* Are you aware of any problems this can cause after a network restart? Thanks, Evan (1 Reply)
Discussion started by: Evan
1 Replies

9. Shell Programming and Scripting

Remove the comment symbol ' from a file.

I want to remove the commented lines in a file identified by ' symbol at the start of each ine. A sample example will be like: Input ----- 'IFerr_flag=0THEN iferr_flag=0then iferr_flag=0then iferr_flag=0then iferr_flag=0then iferr_flag=0then iferr_flag=0then Output -------... (3 Replies)
Discussion started by: joyan321
3 Replies

10. Shell Programming and Scripting

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... (8 Replies)
Discussion started by: learnbash
8 Replies
Login or Register to Ask a Question