LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 01-30-2007, 12:15 AM   #1
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Rep: Reputation: 32
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...
 
Old 01-30-2007, 01:05 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 01-30-2007, 02:28 AM   #3
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
Are you sure about what whitespace is used in the file?
 
Old 01-30-2007, 02:32 AM   #4
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
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.. ????
 
Old 01-30-2007, 02:36 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 01-30-2007, 02:50 AM   #6
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
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.....
 
Old 01-30-2007, 02:52 AM   #7
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
oh.. ya better way thanks matthewg42, but why it is like that....
 
Old 01-30-2007, 02:55 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
Code:
:set list
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.
 
Old 01-30-2007, 03:05 AM   #9
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
ya i saw that why there will be hidden charecters ?????
 
Old 01-30-2007, 03:07 AM   #10
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
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.
 
Old 01-30-2007, 03:10 AM   #11
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
Tab is not exactly hidden - it is usual visual formatting character with non-constant display length.
 
Old 01-30-2007, 03:23 AM   #12
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
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 ???
 
Old 01-31-2007, 02:43 PM   #13
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
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:
  1. Backed up the target file, just in case.
  2. 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.
  3. Used the "-r" option to avoid escaping those parentheses.
  4. 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.
  5. 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.
 
Old 01-31-2007, 02:47 PM   #14
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
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).
 
Old 02-01-2007, 12:26 AM   #15
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
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
 
  


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
sed command not working right ncsuapex Linux - General 2 04-22-2006 05:27 PM
[SOLVED] working on files with sed and pipe angel115 Linux - Newbie 4 10-23-2005 06:15 PM
Sed command in file not working lbauer Programming 5 04-06-2005 12:31 PM
sed not working if value is passed thru a variable containg value suchi_s Programming 7 10-29-2004 07:41 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

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

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