LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-16-2020, 03:57 AM   #1
jodumont
LQ Newbie
 
Registered: Jun 2010
Posts: 4

Rep: Reputation: 0
How to specify optional character with sed


Hi;

I saw this before but unable to find it via google

basically I want to put in one line these sed
sed -i 's|^Port .*|Port 55555|g' > /etc/ssh/sshd_config
sed -i 's|#Port .*|Port 55555|g' > /etc/ssh/sshd_config

I tried
sed -i 's|^[,#]Port .*|Port 55555|g' > /etc/ssh/sshd_config
sed -i 's|[^,#]Port .*|Port 55555|g' > /etc/ssh/sshd_config
sed -i 's|[^\|#]Port .*|Port 55555|g' > /etc/ssh/sshd_config
sed -i 's|^[\|#]Port .*|Port 55555|g' > /etc/ssh/sshd_config
sed -i 's|^[,#]Port .*|Port 55555|g' > /etc/ssh/sshd_config

sed -i 's|^(,#)Port .*|Port 55555|g' > /etc/ssh/sshd_config
sed -i 's|(^,#)Port .*|Port 55555|g' > /etc/ssh/sshd_config
sed -i 's|(^\|#)Port .*|Port 55555|g' > /etc/ssh/sshd_config
sed -i 's|^(\|#)Port .*|Port 55555|g' > /etc/ssh/sshd_config
sed -i 's|^(,#)Port .*|Port 55555|g' > /etc/ssh/sshd_config

sed -i 's|^{,#}Port .*|Port 55555|g' > /etc/ssh/sshd_config
sed -i 's|{^,#}Port .*|Port 55555|g' > /etc/ssh/sshd_config
sed -i 's|{^\|#}Port .*|Port 55555|g' > /etc/ssh/sshd_config
sed -i 's|^{\|#}Port .*|Port 55555|g' > /etc/ssh/sshd_config
sed -i 's|^{,#}Port .*|Port 55555|g' > /etc/ssh/sshd_config


and probably more but never succeed
 
Old 02-16-2020, 04:05 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
I don't really understand what do you want to achieve, but "optional" is a ? in sed
Code:
sed -i 's|#?Port .*|Port 55555|g' > /etc/ssh/sshd_config
 
Old 02-16-2020, 04:33 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
More likely
Code:
sed -i 's|[#^]Port .*|Port 55555|g' > /etc/ssh/sshd_config
Whether the "g" is relevant is highly debatable.
 
Old 02-16-2020, 04:45 AM   #4
jodumont
LQ Newbie
 
Registered: Jun 2010
Posts: 4

Original Poster
Rep: Reputation: 0
thanks for your quick answer but none of these works
I use Ubuntu 18.04LTS

@syg00
your suggestion only works if the line is commented such as '#Port 22' and don't work with 'Port 22'
sed -i 's|[#^]Port .*|Port 55555|g' /etc/ssh/sshd_config
 
Old 02-16-2020, 04:58 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,294
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
You'll need to read up on the sed scripting language see "man sed" and then take a look at a good sed tutorial.

If you are trying to change the port number regardless of whether the line is remmed out or not then make the pound sign optional. An extended regular expression is probably the easiest option there:

Code:
sed -E 's/^#?Port.*/Port 55555/' /etc/ssh/sshd_config
If you are going to use the -i option, you might want to leave a backup copy:

Code:
sed -i.orig -E 's/^#?Port.*/Port 55555/' /etc/ssh/sshd_config
 
1 members found this post helpful.
Old 02-16-2020, 04:58 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
I answered your requirements. You change the requirements, that is a different (though similar) problem. See pan64's suggestion in addition to mine.
You need to make some effort.
 
Old 02-16-2020, 10:41 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,780

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Quote:
Originally Posted by jodumont View Post
thanks for your quick answer but none of these works
I use Ubuntu 18.04LTS

@syg00
your suggestion only works if the line is commented such as '#Port 22' and don't work with 'Port 22'
sed -i 's|[#^]Port .*|Port 55555|g' /etc/ssh/sshd_config
Yes, because [#^] means: a character that is # or ^
Certainly the following was meant:
Code:
sed -i -E 's|(#|^)Port .*|Port 55555|' /etc/ssh/sshd_config
Normally it requires ERE ( -E or -r option).
With GNU sed extensions the following might work as well:
Code:
sed -i 's|\(#\|^\)Port .*|Port 55555|' /etc/ssh/sshd_config
Now it means: a leading # or at the beginning of the line.
The # somewhere can maybe sharpened by require it to be at the beginning of the line. Then it is
Code:
sed -i -E 's|(^#|^)Port .*|Port 55555|' /etc/ssh/sshd_config
or better, at the beginning of the line an optional #
Code:
sed -i -E 's|^#?Port .*|Port 55555|' /etc/ssh/sshd_config
as was given before by Turbocapitalist.
 
  


Reply

Tags
sed bash


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
specify root block device for boot, but specify where? Siljrath Gentoo 2 08-05-2011 10:01 PM
Bash scripting: parsing a text file character-by-character Completely Clueless Programming 13 08-12-2009 09:07 AM
To know the function on checking whether a character is ascii or unicode character. murugesan Programming 2 01-23-2009 01:07 PM
about wide character and multiple byte character George2 Programming 5 05-23-2006 01:03 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:50 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