8

I would like to change the line "disable = yes" to "disable = no" into the following file :

[root@centos2 ~]# cat /etc/xinetd.d/tftp
service tftp
{
    ...
    server_args             = -s /var/lib/tftpboot
    disable                 = yes
    per_source              = 11
    ...
}  

I tried this :

[root@centos2 ~]# grep 'disable                 = yes' /etc/xinetd.d/tftp
[root@centos2 ~]#

by just copying the space with my mouse but it doesn't grep anything...

Why and how can I know what are the elements between "disable" and "=" ? Is it several spaces? tabulations?

I know I can grep using the following regex :

[root@centos2 xinetd.d]# grep -E 'disable.+= yes' /etc/xinetd.d/tftp
    disable                 = yes
[root@centos2 xinetd.d]#

And finaly, is there a better way of replacing "yes" by "no" using sed than the following :

[root@centos2 xinetd.d]# sed -r 's/disable.+= yes/disable                 = 
no/g' /etc/xinetd.d/tftp

service tftp
{
    ...
    server_args             = -s /var/lib/tftpboot
    disable                 = no
    per_source              = 11
    ...
}

EDIT :

Result of the od command thanks @ilkkachu

[root@centos2 xinetd.d]# < /etc/xinetd.d/tftp grep disable | od -c
0000000  \t   d   i   s   a   b   l   e
0000020                                       =       y   e   s  \n
0000037
12

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.

| improve this answer | |
  • It is well explained but doesn't work for me. – Pozinux Nov 20 '17 at 20:47
  • 1
    @Pozinux I didn't see that your disable was not at the beginning of a line. I've amended my RE accordingly. – roaima Nov 20 '17 at 21:02
  • Still no change sorry. – Pozinux Nov 20 '17 at 21:05
  • 2
    You're right, actually -- I can reproduce that. sed '/disable\>/s/\<yes\>/no/' <<<' disable = yes' doesn't work for me either (on MacOS, with BSD sed). The OP's original code works fine for me with GNU sed (v4.4), though. – Charles Duffy Nov 20 '17 at 21:15
  • 1
    Was going to say -- works fine for me with docker run -i centos:latest sed '/disable\>/s/\<yes\>/no/' <<<' disable = yes' – Charles Duffy Nov 20 '17 at 21:23
6

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.

| improve this answer | |
  • Didn't know about od command, very nice! I edited my question so that you can see the result of the od command. If I understand, there are no tabs between the "disable" and the "=" is this right? But how many? Can we count them? – Pozinux Nov 20 '17 at 20:50
  • 1
    @Pozinux, so no tabs then. I can't see any reason why that first grep shouldn't work, sorry – ilkkachu Nov 21 '17 at 10:13
0

Try grep alternative called sift from sift-tool.org - is grep on steriods that does intelligent regex pattern matching

| improve this answer | |
  • How would that work in this situation? Can you give an example? – roaima Nov 22 '17 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.