Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
密钥名的grep json值。(不带选项的总线箱-P)_Json_Linux_Bash - Fatal编程技术网

密钥名的grep json值。(不带选项的总线箱-P)

密钥名的grep json值。(不带选项的总线箱-P),json,linux,bash,Json,Linux,Bash,我找到了很多讨论“如何grep json值”的线程。 但不幸的是,这对我和所有使用busybox(嵌入式linux)grep的人来说都是无用的。这个grep版本没有选项“-P”(perl exp)。只有“-E”(扩展Regexp)可用 BusyBox v1.20.2 () multi-call binary. Usage: grep [-HhnlLoqvsriwFE] [-m N] [-A/B/C N] PATTERN/-e PATTERN.../-f FILE [FILE]... Sear

我找到了很多讨论“如何grep json值”的线程。 但不幸的是,这对我和所有使用busybox(嵌入式linux)grep的人来说都是无用的。这个grep版本没有选项“-P”(perl exp)。只有“-E”(扩展Regexp)可用

BusyBox v1.20.2 () multi-call binary.

Usage: grep [-HhnlLoqvsriwFE] [-m N] [-A/B/C N] PATTERN/-e PATTERN.../-f FILE [FILE]...

Search for PATTERN in FILEs (or stdin)

        -H      Add 'filename:' prefix
        -h      Do not add 'filename:' prefix
        -n      Add 'line_no:' prefix
        -l      Show only names of files that match
        -L      Show only names of files that don't match
        -c      Show only count of matching lines
        -o      Show only the matching part of line
        -q      Quiet. Return 0 if PATTERN is found, 1 otherwise
        -v      Select non-matching lines
        -s      Suppress open and read errors
        -r      Recurse
        -i      Ignore case
        -w      Match whole words only
        -x      Match whole lines only
        -F      PATTERN is a literal (not regexp)
        -E      PATTERN is an extended regexp
        -m N    Match up to N times per file
        -A N    Print N lines of trailing context
        -B N    Print N lines of leading context
        -C N    Same as '-A N -B N'
        -e PTRN Pattern to match
        -f FILE Read pattern from file
我有一个json示例:

{
  "one": "apple",
  "two": "banana"
}
现在,我想从键“one”中提取值,例如“apple”

这只是一个例子

顺便说一句:如何从regex访问组


如果您有任何帮助或其他选择,我将不胜感激。

使用
busybox awk

busybox awk  -F '[:,]' '/"one"/ {gsub("[[:blank:]]+", "", $2); print $2}'
  • -F'[:,]”
    将字段分隔符设置为

  • /“一”/{gsub([[:blank:][]+”,“,”,$2);如果行中包含
    “一”
    ,则打印$2}
    macthes,如果是,则删除第二个字段中的所有水平空白,然后打印该字段

如果您也想去掉引号:

busybox awk  -F '[:,]' '/"one"/ {gsub("[[:blank:]\"]+", "", $2); print $2}'

示例:

$ cat file.json 
{
  "one": "apple",
  "two": "banana"
}

$ busybox awk  -F '[:,]' '/"one"/ {gsub("[[:blank:]]+", "", $2); print $2}' file.json 
"apple"

$ busybox awk  -F '[:,]' '/"one"/ {gsub("[[:blank:]\"]+", "", $2); print $2}' file.json 
apple

我喜欢简单的命令,它可以增强可读性和易懂性。在您的文件中,首先我们必须删除空白以匹配字符串。为此,我通常更喜欢
sed
命令。之后,我们可以使用
awk
命令查找匹配项

awk -F: '$1=="one" {print $2}' | sed -r 's/(\t|\s|,)//g' file.json
它将返回:

"apple"
"apple",
注意:我删除了行末尾的逗号(,)。如果您还需要逗号作为输出,请参阅下面的命令。

awk -F: '$1=="one" {print $2}' | sed -r 's/(\t|\s)//g' file.json
它将返回:

"apple"
"apple",

好的,谢谢。没有grep的解决方案。但它像一个符咒。