shell 如果列值不可用,则JQ字段值向左移动

vshtjzan  于 2023-04-21  发布在  Shell
关注(0)|答案(3)|浏览(144)

我有一个JSON对象,如下所示,我试图把它放在一个表中。

[
  {
    "name": "Jay",
    "Age": "23"
  },
  {
    "name": "Jamie",
    "Age": "32"
  },
  {
    "name": "",
    "Age":"26"
  }
]

问题是,如果某个列的值不可用,下一列的值就会滑入其中。
例如,这是第三个值发生的情况。我如何防止这种情况发生?

|  |name  | Age |
|--|----  | ----|
|1.| Jay  | 23  |
|2.| Jamie| 32  |
|3.|  26  |     |

我的JSON在一个名为listofres的变量中:

echo "$listofres" 
| jq -r '.[] | [.["name"], .["Age"]] | @tsv' 
| awk 'BEGIN { printf "%-2s %-20s %-20s\n", "","Name", "Age"; 
print "---------------------------------------"} 
{printf "%-2s. %-20s %-20s\n", (NR < 10 ? " " : "")NR, $1, $2}'
oo7oh9g9

oo7oh9g91#

给出:

cat file
[
  {
    "name": "Jay",
    "Age": "23"
  },
  {
    "name": "Jamie",
    "Age": "32"
  },
  {
    "name": "",
    "Age":"26"
  }
]

这对于Miller来说是相当简单的:

mlr --ijson --omd cat -n file    # markdown format

图纸:

| n | name | Age |
| --- | --- | --- |
| 1 | Jay | 23 |
| 2 | Jamie | 32 |
| 3 |  | 26 |

该markdown呈现为:
| n|名称|年龄|
| --------------|--------------|--------------|
| 1|杰伊|二十三|
| 二|杰米|三十二|
| 三||二十六|
或者漂亮的印刷品:

mlr --ijson --opprint cat -n file
n name  Age
1 Jay   23
2 Jamie 32
3 -     26

或禁止:

mlr --ijson --opprint --barred cat -n file 
+---+-------+-----+
| n | name  | Age |
+---+-------+-----+
| 1 | Jay   | 23  |
| 2 | Jamie | 32  |
| 3 |       | 26  |
+---+-------+-----+

你也可以使用米勒来处理awk中的JSON(唯一的修改是使用\t作为IFS):

mlr --j2t -N cat file | awk -F "\t" '
BEGIN { printf "%-2s %-20s %-20s\n", "","Name", "Age"; 
print "---------------------------------------"} 
{printf "%-2s. %-20s %-20s\n", (NR < 10 ? " " : "")NR, $1, $2}'

图纸:

Name                 Age                 
---------------------------------------
 1. Jayson               23                  
 2. Jamie                32                  
 3.                      26
wkyowqbh

wkyowqbh2#

$ cat tst.sh
#!/usr/bin/env bash

listofres='[
  {
    "name": "Jay",
    "Age": "23"
  },
  {
    "name": "Jamie",
    "Age": "32"
  },
  {
    "name": "",
    "Age":"26"
  }
]'

printf '%s\n' "$listofres" |
jq -r '.[] | [.["name"], .["Age"]] | @tsv' |
awk '
    BEGIN {
        FS = "\t"
        split(" Name Age",hdr,/[ ]/)
        for ( colNr in hdr ) {
            wids[colNr] = length(hdr[colNr])
        }
    }
    {
        $0 = NR "." FS $0
        for ( colNr=1; colNr<=NF; colNr++ ) {
            wid = length($colNr)
            wids[colNr] = (wid > wids[colNr] ? wid : wids[colNr])
            vals[NR,colNr] = $colNr
        }
    }
    END {
        for ( colNr=1; colNr<=NF; colNr++ ) {
            printf "|%-*s", wids[colNr], hdr[colNr]
        }
        print "|"

        for ( colNr=1; colNr<=NF; colNr++ ) {
            dashes = sprintf("%*s",wids[colNr],"")
            gsub(/ /,"-",dashes)
            printf "|%-*s", wids[colNr], dashes
        }
        print "|"

        for ( rowNr=1; rowNr<=NR; rowNr++ ) {
            for ( colNr=1; colNr<=NF; colNr++ ) {
                printf "|%-*s", wids[colNr], vals[rowNr,colNr]
            }
            print "|"
        }
    }
'
$ ./tst.sh
|  |Name |Age|
|--|-----|---|
|1.|Jay  |23 |
|2.|Jamie|32 |
|3.|     |26 |
dpiehjr4

dpiehjr43#

问题出在对awk的调用上。由于您已经使用了@tsv,因此需要使用awk -F\\t ...或等效的。

相关问题