Why can't I grep this way?

Clash Royale CLAN TAG#URR8PPP
up vote
8
down vote
favorite
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
sed grep
add a comment |Â
up vote
8
down vote
favorite
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
sed grep
add a comment |Â
up vote
8
down vote
favorite
up vote
8
down vote
favorite
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
sed grep
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
sed grep
edited Nov 20 '17 at 20:52
asked Nov 20 '17 at 20:09
Pozinux
3812619
3812619
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
12
down vote
accepted
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.
It is well explained but doesn't work for me.
â Pozinux
Nov 20 '17 at 20:47
1
@Pozinux I didn't see that yourdisablewas 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 withdocker run -i centos:latest sed '/disable>/s/<yes>/no/' <<<' disable = yes'
â Charles Duffy
Nov 20 '17 at 21:23
 |Â
show 8 more comments
up vote
6
down vote
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.
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 firstgrepshouldn't work, sorry
â ilkkachu
Nov 21 '17 at 10:13
add a comment |Â
up vote
0
down vote
Try grep alternative called sift from sift-tool.org - is grep on steriods that does intelligent regex pattern matching
How would that work in this situation? Can you give an example?
â roaima
Nov 22 '17 at 9:06
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
12
down vote
accepted
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.
It is well explained but doesn't work for me.
â Pozinux
Nov 20 '17 at 20:47
1
@Pozinux I didn't see that yourdisablewas 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 withdocker run -i centos:latest sed '/disable>/s/<yes>/no/' <<<' disable = yes'
â Charles Duffy
Nov 20 '17 at 21:23
 |Â
show 8 more comments
up vote
12
down vote
accepted
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.
It is well explained but doesn't work for me.
â Pozinux
Nov 20 '17 at 20:47
1
@Pozinux I didn't see that yourdisablewas 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 withdocker run -i centos:latest sed '/disable>/s/<yes>/no/' <<<' disable = yes'
â Charles Duffy
Nov 20 '17 at 21:23
 |Â
show 8 more comments
up vote
12
down vote
accepted
up vote
12
down vote
accepted
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.
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.
edited Nov 22 '17 at 9:55
Stéphane Chazelas
282k53521854
282k53521854
answered Nov 20 '17 at 20:18
roaima
39.9k546109
39.9k546109
It is well explained but doesn't work for me.
â Pozinux
Nov 20 '17 at 20:47
1
@Pozinux I didn't see that yourdisablewas 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 withdocker run -i centos:latest sed '/disable>/s/<yes>/no/' <<<' disable = yes'
â Charles Duffy
Nov 20 '17 at 21:23
 |Â
show 8 more comments
It is well explained but doesn't work for me.
â Pozinux
Nov 20 '17 at 20:47
1
@Pozinux I didn't see that yourdisablewas 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 withdocker run -i centos:latest sed '/disable>/s/<yes>/no/' <<<' disable = yes'
â Charles Duffy
Nov 20 '17 at 21:23
It is well explained but doesn't work for me.
â Pozinux
Nov 20 '17 at 20:47
It is well explained but doesn't work for me.
â Pozinux
Nov 20 '17 at 20:47
1
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
@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
Still no change sorry.
â Pozinux
Nov 20 '17 at 21:05
2
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
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
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
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
 |Â
show 8 more comments
up vote
6
down vote
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.
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 firstgrepshouldn't work, sorry
â ilkkachu
Nov 21 '17 at 10:13
add a comment |Â
up vote
6
down vote
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.
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 firstgrepshouldn't work, sorry
â ilkkachu
Nov 21 '17 at 10:13
add a comment |Â
up vote
6
down vote
up vote
6
down vote
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.
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.
answered Nov 20 '17 at 20:27
ilkkachu
50.3k677138
50.3k677138
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 firstgrepshouldn't work, sorry
â ilkkachu
Nov 21 '17 at 10:13
add a comment |Â
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 firstgrepshouldn't work, sorry
â ilkkachu
Nov 21 '17 at 10:13
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
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
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
@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
add a comment |Â
up vote
0
down vote
Try grep alternative called sift from sift-tool.org - is grep on steriods that does intelligent regex pattern matching
How would that work in this situation? Can you give an example?
â roaima
Nov 22 '17 at 9:06
add a comment |Â
up vote
0
down vote
Try grep alternative called sift from sift-tool.org - is grep on steriods that does intelligent regex pattern matching
How would that work in this situation? Can you give an example?
â roaima
Nov 22 '17 at 9:06
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Try grep alternative called sift from sift-tool.org - is grep on steriods that does intelligent regex pattern matching
Try grep alternative called sift from sift-tool.org - is grep on steriods that does intelligent regex pattern matching
answered Nov 22 '17 at 8:29
Brandon Haberfeld
211
211
How would that work in this situation? Can you give an example?
â roaima
Nov 22 '17 at 9:06
add a comment |Â
How would that work in this situation? Can you give an example?
â roaima
Nov 22 '17 at 9:06
How would that work in this situation? Can you give an example?
â roaima
Nov 22 '17 at 9:06
How would that work in this situation? Can you give an example?
â roaima
Nov 22 '17 at 9:06
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f405840%2fwhy-cant-i-grep-this-way%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password