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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
01-30-2007, 12:15 AM
|
#1
|
Member
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254
Rep:
|
sed not working...
Hi all,
i wanted to alter an option in the file /etc/xinetd.d/telnet i.e. disable = no to disable = yes... i have used the command...
Code:
sed -i -e 's/^disable = no/disable = yes/' /etc/xinetd.d/telnet
but this is not working .. what is the problem ??/ can anyone help me ???
thanks...
|
|
|
01-30-2007, 01:05 AM
|
#2
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
This should work (the -e is not needed, but that isn't a show stopper).
- What error (if any) do you get?
- Does it work without the -i option (-i is sed 4.* specific)?
If the -i option is the problem, try the 'old fashion' way:
sed 's/^disable = no/disable = yes/' /etc/xinetd.d/telnet > /etc/xinetd.d/telnet.new
mv /etc/xinetd.d/telnet.new /etc/xinetd.d/telnet
Hope this helps.
|
|
|
01-30-2007, 02:28 AM
|
#3
|
Senior Member
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900
Rep:
|
Are you sure about what whitespace is used in the file?
|
|
|
01-30-2007, 02:32 AM
|
#4
|
Member
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254
Original Poster
Rep:
|
Thanks a lot for the reply druuna,
but the methods u said is not working... and it is not showing any error also...(but that option is not changing thats all) and if i use without -i it is printing the contents of the file and exiting.... now what to do.. ????
|
|
|
01-30-2007, 02:36 AM
|
#5
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
Are you sure the whitespace is correct? Perhaps there's a tab after the "disable". Try it like this:
Code:
sed -i 's/^[ \t]*disable[ \t]*=[ \t]*no/disable = yes/'
Note, this is case-sensitive. It should be more robust as far as mixed spaces and tabs is concerned.
|
|
|
01-30-2007, 02:50 AM
|
#6
|
Member
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254
Original Poster
Rep:
|
wow... this is superb raskin thanks a million.... actually the command should be like this
Code:
sed -i -e 's/^ disable = no/ disable = yes/' /etc/xinetd.d/telnet
now it is working...and why it is like this.....
|
|
|
01-30-2007, 02:52 AM
|
#7
|
Member
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254
Original Poster
Rep:
|
oh.. ya better way thanks matthewg42, but why it is like that....
|
|
|
01-30-2007, 02:55 AM
|
#8
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
I looked at the files in /etc/xinetd.d and they contain some control character as ^I. We can verify this by opening the file with vi or vim and setting
this shows all the hidden characters. To strip out the ^I chars you can use sed in this way:
Code:
cat telnet | sed 's/^V^I/ /g' | sed 's/disable = no/disable = yes/'
Note that instead of literally write the ^V^I chars, you have to press Ctrl-V Ctrl-I in sequence. The line above will look like
Code:
cat telnet | sed 's/ / /g' | sed 's/disable = no/disable = yes/'
Edit: Just seen the last posts... sorry for redundancy!
Last edited by colucix; 01-30-2007 at 02:56 AM.
|
|
|
01-30-2007, 03:05 AM
|
#9
|
Member
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254
Original Poster
Rep:
|
ya i saw that why there will be hidden charecters ?????
|
|
|
01-30-2007, 03:07 AM
|
#10
|
Senior Member
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900
Rep:
|
What do you mean "why it is like this"? Why in xinet.d tabs are used? Because they want it to be nicely formatted with slightly different parameter name length and without efforts. Why sed doesn't count ' ' as any whitespace? Because maybe you need just space. Why '[ \t]' works? Because [] means 'one of contained single characters' and '\t' means tab.
|
|
|
01-30-2007, 03:10 AM
|
#11
|
Senior Member
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900
Rep:
|
Tab is not exactly hidden - it is usual visual formatting character with non-constant display length.
|
|
|
01-30-2007, 03:23 AM
|
#12
|
Member
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254
Original Poster
Rep:
|
thanks raskin....ya i should have been more specific.. . what i mean to say was " do i need to count all the spaces every time i need to change the option..???" if say some has intended in other way then " is there a way to skip those spaces and change the options.. say if it is like this in file...
" disable = no" and while using sed, should i have to take care of these spaces ?? or else how can i make "programmer friendly " i.e even though if i do not consider spaces it has to work ???
|
|
|
01-31-2007, 02:43 PM
|
#13
|
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
|
If you want to preserve the spaces/tabs, then:
Code:
cp /etc/xinetd.d /etc/xinetd.d.000
sed -ri 's,^([ \t]*disable[ \t]*=[ \t]*)no,\1yes,' /etc/xinetd.d
I have done 5 things here: - Backed up the target file, just in case.
- Used '(', ')', & '\1' to define a sub-expression in the match portion of the regex & then insert it in the replacement portion -- not only saves typing & mistakes, but preserves the formatting.
- Used the "-r" option to avoid escaping those parentheses.
- Put the 2 options together, but when using "-i" be careful to make it "-ri", not "-ir" -- that would make 'r' the back up file suffix.
- Used ',' for the regex delimiter to make it easier to read. It's a little known, or at least rarely used, fact that even in Perl the '/' is NOT the mandatory delimiter. It can be anything you like -- sed uses the 1st character after the command (here, "s") as the delimiter. Pick whatever you find pleasing & easy to parse visually. for me, that's the comma in most cases.
sed is 1 of those basic commands w/ a lot of depth & power, & it pays to read the man page to know what it can do for you.
|
|
|
01-31-2007, 02:47 PM
|
#14
|
Senior Member
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900
Rep:
|
Even more - it pays to know that 'info sed' is more complete than 'man sed'. But only on large investments (and even larger return, though).
|
|
|
02-01-2007, 12:26 AM
|
#15
|
Member
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254
Original Poster
Rep:
|
Thanks for the reply archtoad6,
still i am in confusion.. say (in the file the indentation is like this..) if there are 10 spaces before disable and 2 spaces after "disable" and 3 spaces after "=" sign and then "no"... now if i want to replace this by "yes" do i need to take care of these spaces in "sed" command. ??? cant i do simply like this.. sed -i -e 's/^disable = no/disable = yes/' /etc/xinetd.d/telne
|
|
|
All times are GMT -5. The time now is 05:02 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|