LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-01-2007, 12:34 AM   #16
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69

You can use
Code:
sed -i -e 's/^disable\s*=\s*no\s*/disable = yes/' /etc/xinetd.d/telne
It takes care of all spaces.

Sorry, I do not know a way to make sed recognize ' ' as '\s*' (which you need).
 
Old 02-01-2007, 08:53 AM   #17
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
Doh!

There is a much easier way to take care of the spaces no matter where they are, or if they vary from 1 instance of the file to another -- use a regex address:
Code:
sed -ri '/^\s*disable\s*=/s,yes,no,' file
Note: it may be the '-r' option, but '\s*' works for me:
Code:
$ bash --version
GNU bash, version 3.00.16(1)-release (i386-pc-linux-gnu)

$ sed --version
GNU sed version 4.1.4
YMMV

If you want to be really thorough, then maybe:
Code:
sed -ri '\#^\s*disable\s*=\s*(yes|no)\s*$#s,yes,no,' file
BTW, I did test both code lines on a dummy file on my own machine.

Analysis
The real problem is 2-part:
  1. Find the "disable" line,
  2. change "yes" to "no".
Once we know we have the "disable" line, the only thing in it we need to change is the "yes". Including all the allowable forms of "disable" in the replacement command is unnecessary. The regex address is exactly what's needed, why I didn't think of it 1st time 'round, I don't know. What we want/need to do is find all forms of the line that are legal in the syntax of the file. We are creating, in effect, a micro-parser for this syntax. We also do not want to change the visual alignment chosen by the author of the specific file we are working on. Splitting the line search from the word replacement does this very nicely.

In my 2nd snippet, I have again used the "any character can be the command delimiter feature, & showed the regex address equivalent, also.

I hope this isn't too much info, my excuses are: a) those who come after, & b) I'm getting ready to teach a Linux CLI class in a very few weeks.
 
Old 02-02-2007, 06:03 AM   #18
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
[QUOTE=archtoad6]use a regex address:
Code:
sed -ri '/^\s*disable\s*=/s,yes,no,' file
ah, the use of the comma as separator confused me but it is equivalent to:
Code:
sed -ri '/^\s*disable\s*=/s/yes/no/' file
OR
sed -ri '/^\s*disable\s*=/s#yes#no#' file
OR
sed -i '/^[[:space:]]*disable[[:space:]]*=/s#yes#no#' file
The last one uses the character class [[:space:]] for spaces and tabs, which makes it a bit more clear, imo.
 
Old 02-02-2007, 06:28 AM   #19
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
woooow... this is really awwwwwesome archtoad6.. thanks a million its working for me...
Code:
sed -ri '/^\s*disable\s*=/s,yes,no,' file
... and please can u tell each and every option in the code how it works (i am really not familiar with sed)??? especially regex...
Thanks all for replies...

Last edited by culin; 02-02-2007 at 06:31 AM.
 
Old 02-02-2007, 02:41 PM   #20
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
Quote:
Originally Posted by culin
Code:
sed -ri '/^\s*disable\s*=/s,yes,no,' file
... and please can u tell each and every option in the code how it works
Here goes

Slashes in the beginning of the command are used to match lines with regexp, so: /foo/ would match all lines containing foo.

Next we do an action on the lines we match, in this case 'Substitute'.
If you want to substitute on all lines you will see something like:
sed 's/mouse/cat/' file
That would replace mouse with cat.
Archtoad6 is using comma's as separators instead of slashes, basicly you can use alot of characters, like s### s,,, s///
So now we have:
/match a line/ action on that line is S,yes,no

All lines that have foo, replace mouse with cat:
sed '/foo/s/mouse/cat/' file

Which lines do we match?
^ matches the beginning of a line.
\s is extended regexp for a space character, tabs/spaces.
* means multiple
\s* heh! Multiple space characters
So ^\s*disable\s* will match a series of space, followed by disable and a series of space. (and nothing is allowed in front of it).

Options for sed:
-r To use extended regexp, like \s
-i To edit files in place, so it will edit your file.
If you don't use -i, it will output the output to your screen instead of to the file. Always start by not using -i, look at the output. If the output is ok than use -i to edit the file.

If that does not help I do't know what to do
Muha
 
Old 02-03-2007, 11:31 AM   #21
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
Nice explanation, muha. Thanks. Sorry I didn't log in yesterday & answer myself.

I wish there were a short & polite form of "RTFM", perhaps "PRTMP" ("Please ... Page"), because there are many valuable man pages that bear not only reading, but re-reading. sed is one of those. I don't know how many times I have re-read that page to check some point, but I only recently realized the power (== danger, also) of the '-i' option, especially coupled w/ KWrite. I had a text file in KWrite, but there were a few things its regex Replace could not do, so I opened a Kosole & ran the necessary sed commands (w/ '-i'). When I returned to KWrite, it asked if I wanted to reload the file or ignore the change. Neat!

1 safety feature to note about sed -i: whatever you put after the 'i' becomes the suffix for the back up file, so you can easily save the original. Careful, though, the default is to make no back up.


Why I . . .
  • Use commas, not slashes:
    "Readability" -- For me, & this is definitely personal, I find that commas make the elements of the regex stand out on the page. That allow me to more easily understand the logic "at a glance"; especially when the regex includes escaped (i.e. w/ a '\') expressions; even more so when the escaped character(s) is/are slash(es) -- e.g. "s/\/\/\//\/\//"*. I like to call these "kung fu regexes" to accuse them, in a humorous way, looking like a Chuck Norris sparring w/ David Carradine.

  • Normally use '-r' w/ sed:
    Syntax consistency -- As explained by muha, it causes the use of "extended" regexes. This makes the syntax closer to egrep & awk. I.e. fewer minor distinctions to remember.


* BTW, "s/\/\/\//\/\//" == "s,///,//," == "s!///!//!", which is easiest to figure out?
(I added the exclamation point example because the text is punctuated w/ commas.)
 
Old 02-05-2007, 12:06 AM   #22
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
aw aw aw.... damn good explanation..... thanks a lot lot muha and archtoad6,, i am sure that i couldnt have get this clarity of info in books or man pages.... thanks for sharing ur experiences....
 
Old 02-05-2007, 08:55 AM   #23
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
culin, You're welcome. But also, thank you for opportunity to keep & improve my programming skills, as well as think about an interesting problem.

muha, I already said (deserved) nice things about your post #20, but I never commented on post #18. Your point about '[[:space:]]' vs. '\s' is well taken. Especially in a sed address regex, where it ('\s') obscures the 's' that is the beginning of the command. This is a classic trade off between brevity & clarity. I tend toward '\s', at least when I use it, and it's probably fine for personal 1-time stuff; but I think that in a permanent script, whether personal or public, that '[[:space:]]' might be advisable.

If this were a programming class, I could see homework questions here:
  1. Is '[[:space:]]' exactly the same as '\s' in sed?

  2. Is this true in:
    1. grep,
    2. egrep,
    3. awk

  3. Have the best of both worlds -- brevity when typing, clarity when reading -- write a sed script which replaces '\s' w/ '[[:space:]]' in other scripts:
    1. in sed
    2. in awk
    3. in bash scripts to all lines containing:
      1. sed,
      2. awk,
      3. egrep,
      4. grep,
      as appropriate.

    1. What is the difference between '[[:space:]]' & '[[:blank:]]'?
    2. '[[:space:]]' isn't mentioned in man regex(7), when is it valid?


Discussion/"Answers"

1. - Since '[[:space:]]' includes characters not found on a single line (see 4. below), a better Q might be:
"Is '[[:blank:]]' exactly the same as '\s' in sed?"
I think the definitive A to these Q's lies in find the documentation for '\s'. Unfortunately, I can't find any. There is no mention here: http://www.gnu.org/software/sed/manu.../sed.html#SEC5. From there, I went to: http://www.gnu.org/software/gawk/manual/gawk.html. I searched for "\s" & found nothing. I checked: 2.2 Escape Sequences & 2.5 gawk-Specific Regexp Operators, I searched on '\a' & found "2.2 Escape Sequences" -- as I expected.

BTW, All the all-on-1-html-page GNU manuals are very useful for at least 2 reasons: 1 search covers the whole thing & you don't have to fight the (IMNRHO abominable) info system.
sed: http://www.gnu.org/software/sed/manu..._mono/sed.html
awk: http://www.gnu.org/software/gawk/manual/gawk.html


2. - I did some quick tests that seem to indicate that all 3 are the same in sed, grep, egrep, & awk, when working in a single line context. (Again w/o definitive documentation, we can't be sure about '\s'.)

2. & 3c. -- Found this in "info:/grep/Regular Expressions" ("info:..." is a Konqueror ioslave):
Quote:
5.5 Basic vs Extended
=====================
In basic regular expressions the metacharacters `?', `+', `{', `|',
`(', and `)' lose their special meaning; instead use the backslashed
versions `\?', `\+', `\{', `\|', `\(', and `\)'.

Traditional `egrep' did not support the `{' metacharacter, and some `egrep' implementations support `\{' instead, so portable scripts should avoid `{' in `egrep' patterns and should use `[{]' to match a literal `{'.

GNU `egrep' attempts to support traditional usage by assuming that `{' is not special if it would be the start of an invalid interval specification. For example, the shell command `egrep '{1'' searches for the two-character string `{1' instead of reporting a syntax error in the regular expression. POSIX.2 allows this behavior as an extension, but portable scripts should avoid it.
There is similar language in the man page.


3. -- I'll leave this to a community discussion.


4. -- Well, '[[:space:]]' may not be mentioned in man regex(7), but it is in info regex at "info:/regex/Character Class Operators" (in Konqueror). There, it says:
Quote:
'space'
space, carriage return, newline, vertical tab, and form feed
Quote:
'blank'
system-dependent; for GNU, a space or tab
Now, my Q still is: where did we hear about '\s'? We know it works, but I can't quickly find it documented.


Edit: Additions & rearrangements to "Discussion", no one posted in the mean time. (Had to log out & return.)
(post 1204)
4750+ chars., a personal record -- almost double any previous

Last edited by archtoad6; 02-05-2007 at 10:33 AM.
 
Old 02-05-2007, 05:24 PM   #24
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
Answer: Vim rules!
:help \\s -> pattern.txt...
 
Old 02-05-2007, 06:01 PM   #25
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400
Hi,

Quote:
Now, my Q still is: where did we hear about '\s'? We know it works, but I can't quickly find it documented.
\s is a posix regular expression notation.

Take a look at man 1 perlre, as stated in the sed manpage

From perlre:

\s Match a whitespace character
\S Match a non-whitespace character

space \s [2]
[2] Not exactly equivalent to "\s" since the "[[:space:]]" includes also the (very rare) `vertical tabulator', "\ck", chr(11).


\s is also mentioned in the book Mastering Regular Expressions by O'reilly. It's first mention (page 42) describes it as: Generic "whitespace" with \s.

BTW: The answer to question number one is: No

Last edited by druuna; 02-05-2007 at 06:03 PM.
 
Old 02-06-2007, 03:45 AM   #26
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
huh...!!! HiFi discussions... atleast i got 10% of that..and thanks for those good links archtoad6,
 
Old 02-07-2007, 04:58 PM   #27
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
druuna, tnx for the explanation.
 
  


Reply


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
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 04:08 AM.

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