LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-25-2017, 03:15 PM   #1
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 237
Blog Entries: 2

Rep: Reputation: Disabled
How to commet out specific lines of text?


Hello ladies and gents, im trying to commet out tty2-tty63 in /etc/securetty (to disable unneeded terminals for security reasons). But everything that I have previously tried has failed to work.. Any ideas would be much appreciated, thank you! Btw this is what I already tried.
Code:
for i in `cat /etc/securetty|grep tty[2-63]` {1..63}; do
echo "#" >> $i
done
And im sure that's written completely wrong.

Last edited by justmy2cents; 07-26-2017 at 10:09 AM.
 
Old 07-25-2017, 04:50 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Not even in the ballpark!

Do you have to do it with a script? I think I would open an editor and search, replace those line and be done in 3 seconds.

If you need a scripted solution, sed would be the tool, maybe something like this...

Code:
sed -r 's/^tty([2-9]|1[0-9])/#&/' /etc/securetty
 
Old 07-25-2017, 05:03 PM   #3
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 237

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled
Lol I tried.. Anywho thanks, and yeah it's going to be part of a script that does other configuration changes that I don't really want to do by hand, partly cause im a slow typer.
 
Old 07-25-2017, 06:53 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
I think a little differently - specify what you want to keep (one line) and comment everything else rather than have to construct potentially ever more complex regex.
Code:
 sed -r '/^tty1$/! s/^/#/' /etc/securetty
If you need to accommodate whitespace before the end of record, trivial to add.
 
Old 07-26-2017, 12:22 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
This should be pretty safe
Code:
file=/etc/securetty
if [ -f $file ]; then
  cp -p $file $file.orig &&
  sed '/^tty1$/!s/^tty[1-9][0-9]*/#&/' $file.orig >$file
fi
It leaves a .orig file behind.
 
Old 07-26-2017, 10:16 AM   #6
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 237

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
Not even in the ballpark!

Do you have to do it with a script? I think I would open an editor and search, replace those line and be done in 3 seconds.

If you need a scripted solution, sed would be the tool, maybe something like this...

Code:
sed -r 's/^tty([2-9]|1[0-9])/#&/' /etc/securetty
Thanks worked like a charm.
 
Old 07-26-2017, 10:18 AM   #7
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 237

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
I think a little differently - specify what you want to keep (one line) and comment everything else rather than have to construct potentially ever more complex regex.
Code:
 sed -r '/^tty1$/! s/^/#/' /etc/securetty
If you need to accommodate whitespace before the end of record, trivial to add.
I think both modes of thinking are useful, so thanks that helps too!
 
Old 07-26-2017, 10:20 AM   #8
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 237

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled
Quote:
Originally Posted by MadeInGermany View Post
This should be pretty safe
Code:
file=/etc/securetty
if [ -f $file ]; then
  cp -p $file $file.orig &&
  sed '/^tty1$/!s/^tty[1-9][0-9]*/#&/' $file.orig >$file
fi
It leaves a .orig file behind.
I keep this one in mind if I need to do this in a work environment or something, but im just doing it in a live install so I not too worried about messing up the file.. Thanks bro!

Last edited by justmy2cents; 07-26-2017 at 11:28 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Comet Ison Pedroski Linux - Software 18 11-29-2013 03:15 PM
[SOLVED] change a specific characters to capital in a specific text ramzaher Linux - Newbie 12 12-03-2010 04:50 AM
SuSE 9.3 - searching for specific text in file - Command line options? EnderX Linux - Newbie 1 04-15-2010 01:45 PM
bash script to apply sed command only to a specific text area mauran Programming 6 07-13-2007 04:38 PM
Command line tools to Find files with specific text naps Linux - Software 5 11-15-2004 04:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration