How to comment a specific line of a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to comment a specific line of a file?
# 1  
Old 09-17-2014
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, thanks
Code:
awk '/requiretty/{print "#",$0}1' /etc/sudoers >/etc/sudoers.new

# 2  
Old 09-17-2014
Hello prvnrk,

Following may help you in same.(not tested though)

Code:
awk '/Defaults requiretty/ {$0="#"$0} 1' file_name > Output_file_name

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-17-2014 at 07:43 AM.. Reason: Adding Output file
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 09-17-2014
Thanks RavinderSingh,

Unfortunately your solution did NOT work if I used as it is.

Code:
[root@test_srvr tmp]# cat test_file
Defaults    requiretty
# changed in order to be able to use sudo without a tty. See requiretty above.

Code:
[root@test_srvr tmp]# awk '/Defaults requiretty/ {$0="#"$0} 1' test_file
Defaults    requiretty
# changed in order to be able to use sudo without a tty. See requiretty above.

Code:
[root@test_srvr tmp]# gawk '/Defaults requiretty/ {$0="#"$0} 1' test_file
Defaults    requiretty
# changed in order to be able to use sudo without a tty. See requiretty above.
[root@test_srvr tmp]#


But it worked if I use only requiretty but it's adding # to all the lines that has requiretty. I think it's due to variable spaces between Defaults and requiretty. Is there any way to discard number of spaces between them and check?

Code:
[root@test_srvr tmp]# gawk '/requiretty/ {$0="#"$0} 1' test_file
#Defaults    requiretty
## changed in order to be able to use sudo without a tty. See requiretty above.


Last edited by prvnrk; 09-17-2014 at 08:05 AM..
# 4  
Old 09-17-2014
Hello prvnrk,

You can use following.

Code:
awk '/^Defaults.*requiretty$/ {$0="#"$0} 1' file_name

It is only for lines which are starting from Defaults and ending for requiretty. If this doesn't fulfills your requirement kindly let us know the complete request.

Thanks,
R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to comment a specific line of a file in Solaris 10?

Hi Folks, sed -i '3s/^/#/' abc.txt is commenting the line number 3 in LINUX Not Working in Solaris 10 or higher sed -i sed -i '3s/^/#/' abc.txt sed: illegal option -- i i have a file as below cat abc.txt bc vdv shhsd cdc skdk Please advise (15 Replies)
Discussion started by: abhaydas
15 Replies

2. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

3. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

4. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

5. Shell Programming and Scripting

comment a line of the patterns is a the beginning of the line

I need to comment the lines starting with pattern "exclude" or "exclude=". If the work exclude comes at any other part, ignore it. Also, ignore, excludes, excluded etc. Ie only comment the line starting with exclude. File contents. exclude exclude= hi I am excluded excludes excludes= ... (9 Replies)
Discussion started by: anil510
9 Replies

6. Shell Programming and Scripting

Using awk to read a specific line and a specific field on that line.

Say the input was as follows: Brat 20 x 1000 32rf Pour 15 p 1621 05pr Dart 10 z 1111 22xx My program prompts for an input, what I want is to use the input to locate a specific field. Like if I type in, "Pou" then it would return "Pour" and just "Pour" I currently have this line but it is... (6 Replies)
Discussion started by: Bungkai
6 Replies

7. Shell Programming and Scripting

uncomment or comment one specific line in a config file

Hello. I want comment or uncomment a ligne in a config file. The file name : /etc/samba/smb.conf Normaly the ligne is uncomment :so the line begin with a tab character followed by passdb backend =\tpassdb backend = In that case I should comment this line ... (2 Replies)
Discussion started by: jcdole
2 Replies

8. Shell Programming and Scripting

Comment a line with SED

I have around 25 hosts and each hosts has 4 instance of jboss and 4 different ip attached to it . I need to make some changes to the startup scripts. Any tips appreciated. I have total of 100 instances which bind to 100 different ip address based on instance name. For example File1 ... (1 Reply)
Discussion started by: gubbu
1 Replies

9. UNIX for Dummies Questions & Answers

how to replace a text of line with a comment line

I want to replace this line : "test compare visible] true" and make it "#test compare visible] true". How can I do it ? And it should be checked in many sub folder files also. (6 Replies)
Discussion started by: manoj.b
6 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