0个或多个空格的grep正则表达式

grep regex of 0 or many spaces

我想基于正则表达式对文件进行 grep。

在正则表达式中,我想在 spcace 或 none space 上进行比较,所以我尝试了以下表达式:/ {0,}/ 但它不起作用。

在我的整个正则表达式之后

cat test.txt  | grep "\"cache_changes\":/ {0,}/\"xax.yby.zcz[ \"]"

我该怎么做?

在我的文件之后 test.txt:

{ "nbr": "01", "parameter": "toto.aaa.1.bbb", "value": "45", "notification": "0", "cache_changes":"xax.yby.zcz" "set_cmd": "any_f xax.yby.zcz $val" }
{ "nbr": "02", "parameter": "toto.eeee.1.bbb", "value": "45", "notification": "0", "cache_changes": "xax.yby.zcz xa2x.yb2y.zc2z" "set_cmd": "any_f xax.yby.zcz $val" }
{ "nbr": "03", "parameter": "toto.aaa.2.bbb", "value": "45", "notification": "0", "cache_changes": "xa2x.yb2y.zc2z xax.yby.zcz" "set_cmd": "any_f xax.yby.zcz $val" }
{ "nbr": "04", "parameter": "toto.ffff.3.bbb", "value": "45", "notification": "0", "cache_changes": "xa2x.yb2y.zc2z xax.yby.zcz xa3x.yb3y.zc3z" "set_cmd": "any_f xax.yby.zcz $val" }
{ "nbr": "05", "parameter": "titi.aaa.1.bbb", "value": "45", "notification": "0", "cache_changes": "xdx.yey.zfz" "set_cmd": "any_f xax.yby.zcz $val" }
{ "nbr": "06", "parameter": "titi.eeee.1.bbb", "value": "45", "notification": "0", "cache_changes": "xdx.yey.zfz xd2x.ye2y.zf2z" "set_cmd": "any_f xax.yby.zcz $val" }
{ "nbr": "07", "parameter": "titi.aaa.2.bbb", "value": "45", "notification": "0", "cache_changes": "xd2x.ye2y.zf2z xdx.yey.zfz" "set_cmd": "any_f xax.yby.zcz $val" }
{ "nbr": "08", "parameter": "titi.ffff.3.bbb", "value": "45", "notification": "0", "cache_changes": "xd2x.ye2y.zf2z xdx.yey.zfz xd3x.ye3y.zf3z" "set_cmd": "any_f xax.yby.zcz $val" }

期望的输出是:

{ "nbr": "01", "parameter": "toto.aaa.1.bbb", "value": "45", "notification": "0", "cache_changes":"xax.yby.zcz" "set_cmd": "any_f xax.yby.zcz $val" }
{ "nbr": "02", "parameter": "toto.eeee.1.bbb", "value": "45", "notification": "0", "cache_changes": "xax.yby.zcz xa2x.yb2y.zc2z" "set_cmd": "any_f xax.yby.zcz $val" }

使用*重复前一个标记零次或多次)而不是重复量词{}

$ grep "\"cache_changes\": *\"xax.yby.zcz[ \"]" file
{ "nbr": "01", "parameter": "toto.aaa.1.bbb", "value": "45", "notification": "0", "cache_changes": "xax.yby.zcz" "set_cmd": "any_f xax.yby.zcz $val" }
{ "nbr": "02", "parameter": "toto.eeee.1.bbb", "value": "45", "notification": "0", "cache_changes": "xax.yby.zcz xa2x.yb2y.zc2z" "set_cmd": "any_f xax.yby.zcz $val" }

如果你真的想使用那个量词,你需要转义大括号,比如

$ grep '"cache_changes": \{0,\}"xax.yby.zcz[ "]' file
{ "nbr": "01", "parameter": "toto.aaa.1.bbb", "value": "45", "notification": "0", "cache_changes": "xax.yby.zcz" "set_cmd": "any_f xax.yby.zcz $val" }
{ "nbr": "02", "parameter": "toto.eeee.1.bbb", "value": "45", "notification": "0", "cache_changes": "xax.yby.zcz xa2x.yb2y.zc2z" "set_cmd": "any_f xax.yby.zcz $val" }

试试这个命令:

grep "\"cache_changes\": *\"xax.yby.zcz[ \"]" test.txt

根据问题提供的测试文件,我得到的是:

{ "nbr": "01", "parameter": "toto.aaa.1.bbb", "value": "45", "notification": "0", "cache_changes": "xax.yby.zcz" "set_cmd": "any_f xax.yby.zcz $val" }
{ "nbr": "02", "parameter": "toto.eeee.1.bbb", "value": "45", "notification": "0", "cache_changes": "xax.yby.zcz xa2x.yb2y.zc2z" "set_cmd": "any_f xax.yby.zcz $val" }