Why can't I grep this way?
The spaces are more commonly known as "whitespace", and can include not just spaces but tabs (and other "blank" characters). In a regular expression you can often refer to these either with [[:space:]]
or \s
(depending on the RE engine) which includes both horizontal (space, tab and some unicode spacing characters of various width if available) for which you can also use [[:blank:]]
and sometimes \h
and vertical spacing characters (like line feed, form feed, vertical tab or carriage return). [[:space:]]
is sometimes used in place of [[:blank:]]
for its covering of the spurious carriage return character in Microsoft text files.
You cannot replace with grep
- it's just a searching tool. Instead, to replace the yes
with no
you can use a command like this:
sed '/disable\>/s/\<yes\>/no/' /etc/xinetd.d/tftp
This tells sed
to substitute (change) the word yes
into no
on any line that contains the word disable
. (The \>
(initially a ex
/vi
regexp operator), in some sed
implementations, forces an end-of-word (though beware it's not whitespace-delimited-words, it would also match on disable-option
)). Conveniently this sidesteps the issue of whitespace altogether.
Be careful: with a line such as eyes yes
, an unbounded yes
substitution would apply to the first instance of yes
and leave you with eno yes
. That's why I have used \<yes\>
instead of just yes
.
Why and how can I know what are the elements between "disable" and "=" ? Is it several spaces? tabulations?
Probably tabs. You could use something like
< /etc/xinetd.d/tftp grep disable | od -c
to see. It will show tabs as \t
, and spaces as spaces.