AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题

问题[array](unix)

Martin Hope
Porcupine
Asked: 2024-05-07 04:27:53 +0800 CST

jq 跳过 null 迭代

  • 8

我用的是jq 1.7

数据

我正在对 yt-dlp 下载的 info.json 进行操作

yt-dlp --write-info-json --skip-download https://www.youtube.com/watch?v=vlIO-7Rpi7c

JQ-任务

  • 现在我想删除字幕中名称不是的所有数组de,en-US并在其中de选择en-US所有带有 element 的对象ext == vtt。

这成功地工作如下:

jq 'pick(.subtitles | .de[],."en-US"[] | select (.ext == "vtt")) | del(..|nulls)' *.json

输出

{
  "subtitles": {
    "de": [
      {
        "ext": "vtt",
        "url": "https://www.youtube.com/api/timedtext?v=vlIO-7Rpi7c&ei=JS05ZoT2Ftf0i9oP8dy4mAo&caps=asr&opi=112496729&xoaf=5&hl=en&ip=0.0.0.0&ipbits=0&expire=1715048341&sparams=ip%2Cipbits%2Cexpire%2Cv%2Cei%2Ccaps%2Copi%2Cxoaf&signature=AB2ECEEABF48D1A16ADC7ACCB8A072D63EE12DCC.5F56904251766FB68624A68FF2BBB57B9CCFFD2F&key=yt8&lang=de&fmt=vtt",
        "name": "German"
      }
    ],
    "en-US": [
      {
        "ext": "vtt",
        "url": "https://www.youtube.com/api/timedtext?v=vlIO-7Rpi7c&ei=JS05ZoT2Ftf0i9oP8dy4mAo&caps=asr&opi=112496729&xoaf=5&hl=en&ip=0.0.0.0&ipbits=0&expire=1715048341&sparams=ip%2Cipbits%2Cexpire%2Cv%2Cei%2Ccaps%2Copi%2Cxoaf&signature=AB2ECEEABF48D1A16ADC7ACCB8A072D63EE12DCC.5F56904251766FB68624A68FF2BBB57B9CCFFD2F&key=yt8&lang=en-US&fmt=vtt",
        "name": "English (United States)"
      }
    ]
  }
}

问题

对于某些Youtube视频,json文件不包含“en-US”而是包含“en”,而有些则包含de、en、en-US这三个。所以我写了以下内容

jq 'pick(.subtitles | .de[],.en[],."en-US"[] | select (.ext == "vtt")) | del(..|nulls)' *.json

错误

jq: 错误 (at:69): 无法迭代 null (null)

需要帮助

如果 json 文件中并非所有这些语言都存在,如何仅选择 .ext == "vtt" 仅适用于 en、en-US、de 语言?

array
  • 1 个回答
  • 255 Views
Martin Hope
poppycock
Asked: 2024-02-16 12:06:29 +0800 CST

在 TCL 中迭代数组,其中数组名称具有数字后缀

  • 5

在TCL中,我有一些名称带有数字后缀的数组(即名称以数字结尾),如下所示:

array set ps0 [ list 0 15.885 1 55.43 1 0.254 2 0.227 3 0.177 ]
array set ps1 [ list 0  6.585 1 56.43 1 0.254 2 0.227 3 0.177 ] 
array set ps2 [ list 0 32.485 1 43.13 1 0.254 2 0.227 3 0.177 ] 

我需要在 TCL 中迭代这些并获取值,但是,无论我做什么来转义数字后缀,我都无法同时获取数组和内容。我尝试过的一切,例如:

ps$i($i)
"ps$i($i)"

甚至使用类似的集合:

set p ps$i

...无法获取具有数字索引的数组。我收到错误:

Original error: can't read "ps": no such variable

...所有可能的组合。我怎样才能做到这一点(在 TCL 中)?

array
  • 2 个回答
  • 28 Views
Martin Hope
Mike Pierce
Asked: 2022-06-04 06:51:40 +0800 CST

GNU bc 中数组索引/下标的上限是多少?

  • 1

我发现 GNU bc中的数组中的下标/索引有一个上限。以交互方式运行并要求让arr[100000000]=42返回错误:

 Runtime error (func=(main), adr=17): Array arr subscript out of bounds.

此数组大小限制未列在 bc 的限制中,并且似乎 32767 的“变量名”限制不会影响这一点,因为 bcarr[100000]=42毫无怨言地接受了。bc 的数组下标/索引的确切界限是什么?有没有办法改变这个界限?

array size
  • 2 个回答
  • 47 Views
Martin Hope
Dirk Horsten
Asked: 2022-06-03 08:00:11 +0800 CST

从 shell 脚本调用命令,传递大多数参数,允许带空格的参数

  • 1

我有一个批量运行 SAS 代码run_sas.sh的命令的包装器。sas典型的调用如下所示

./run_sas.sh -sysin /my_code/my_program.sas -log /my_log_folder/my_program.log
  • run_sas.sh将所有参数传递给saswith ./sas $*。
  • sas然后运行/my_code/my_program.sas并将日志写入/my_log_folder/my_program.log.
  • 然后run_sas.sh分析它被调用的参数
  • 并将日志复制到/admin/.hidden_log_folder/my_program_<today's date>.log

我想做两个改变:

启用多字参数

有些客户绝对希望我在文件夹和文件名中使用空格并要求我运行/their code/their program.sas,所以如果我运行

./run_sas.sh -sysin "/their code/their program.sas" -log "/their log folder"

/their code/their program.sas并且/their log folder应该将单个参数传递给sas

删除特定参数

有时我需要运行./sas_utf8而不是./sas我懒得维护第二个脚本,所以我想允许一个额外的参数,这样

./run_sas.sh -sysin /my_code/my_program.sas -log /my_log_folder -encoding utf8

会打电话

./sas_utf8 -sysin /my_code/my_program.sas -log /my_log_folder

代替

./sas -sysin /my_code/my_program.sas -log /my_log_folder

我该怎么做,最好是在ksh?

ksh array
  • 1 个回答
  • 252 Views
Martin Hope
cryptograthor
Asked: 2022-05-30 14:16:47 +0800 CST

在 zsh 中为 gsettings 键绑定自动化分配一个数组值

  • 1

我认为这个脚本应该可以设置键绑定,但它目前很无聊——更新:它已经被取消了,现在可以工作了。不过它很丑,我正在寻求使它更优雅的方法,因为每次我想要一个新的键绑定时都需要复制粘贴四行。

我声称由于 zsh 不支持 2d 数组,我可能最好在 python 中重写它......更新,python 脚本就在这里

如何正确设置键绑定的问题就在这里。

#!/bin/zsh
# How many keybinds?
N=16
# Put keybindings here
k0=("keyboard dvorak nocaps" "Menu" "setxbmap dvorak -option nocaps")
k1=("launch code" "<Super>c" "code")
k2=("dropdown firefox" "<Shift><Super>f" "tdrop -ma -h \"100%\" -w \"50%\" firefox")

mkeys="org.gnome.settings-daemon.plugins.media-keys"
_locstr="/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom"
_ck="$mkeys.custom-keybinding:$_locstr"
M="$(expr $N - 1)"

# Create spaces for $N keybindings, emulating the command:
# gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "['/org/gnome/settings-daemon/plugins/ media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ ']"
locations=""
for i in $(seq 0 $M); do
  locations+="'$_locstr$i/', "
done
locations+="'$_locstr$N/' "
gsettings set $mkeys custom-keybindings "[$locations]"


# issue commands. Have to paste 4 lines each time, kinda gross.
ck="${_ck}0/"
gsettings set $ck name    $k0[1]
gsettings set $ck binding $k0[2]
gsettings set $ck command $k0[3]
ck="${_ck}1/"
gsettings set $ck name    $k1[1]
gsettings set $ck binding $k1[2]
gsettings set $ck command $k1[3]
ck="${_ck}2/"
gsettings set $ck name    $k2[1]
gsettings set $ck binding $k2[2]
gsettings set $ck command $k2[3]

# test with:
# i=0
# ck="${_ck}$i/"
# gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings
# gsettings list-recursively $ck
zsh array
  • 1 个回答
  • 77 Views
Martin Hope
chzzh
Asked: 2022-05-22 01:32:48 +0800 CST

如何附加到具有动态变量名的数组?

  • 1
section_example=(one two three)
name=example; section_$name+=(four)

bash: syntax error near unexpected token `four'

部分名称事先是未知的。eval并declare -a输出相同的错误。我看到的唯一方法是声明一个带有部分名称和值的关联数组。

bash array
  • 1 个回答
  • 240 Views
Martin Hope
fuumind
Asked: 2022-05-02 00:37:24 +0800 CST

在bash中,如何在没有循环的情况下获取数组最后一个元素的索引

  • 8

在 bash 中,是否可以在不遍历整个数组的情况下获取数组最后一个元素的索引(可能是稀疏的),如下所示:

a=( e0 e1 ... )
i=0
while [ "$i" -lt $(( ${#a[@]} - 1 )) ]
do
  let 'i=i+1'
done
echo "$i"

至少从 bash v 4.2 开始,我可以使用获取数组中最后一个元素的值

e="${array[-1]}"

但这不会让我得到正指数,因为其他元素可能具有相同的值。

bash array
  • 2 个回答
  • 1393 Views
Martin Hope
Rafal Niznik
Asked: 2022-04-20 05:25:10 +0800 CST

Ansible 将字典转换为字典/数组的混合

  • 1

我使用以下文件:变量文件:db.yml

x86_64:  
  alpine:
    version: 3.15.0
  debian:
    version: 11.3.0

aarch64:
  alpine:
    version: 3.15.0
  debian:
    version: 11.3.0

剧本:剧本.yml

---
- name: "Playbook" 
  hosts: localhost
  connection: local
  gather_facts: no

  tasks:
  - name: Import variables
    ansible.builtin.include_vars:
      file: db.yml
      name: db

  - name: DENUG >>> db
    debug:
      var: db


  - name: DENUG >>> db
    debug:
      var: db[item].alpine
    loop: "{{ db.keys()|list }}"
    when: db[item].alpine is defined

输出:

ok: [localhost] => (item=x86_64) => {
    "ansible_loop_var": "item",
    "db[item].alpine": {
        "version": "3.15.0"
    },
    "item": "x86_64"
}
ok: [localhost] => (item=aarch64) => {
    "ansible_loop_var": "item",
    "db[item].alpine": {
        "version": "3.15.0"
    },
    "item": "aarch64"
}

现在我已将 db.yml 转换为新样式,如下所示:

x86_64:
  - distribution: alpine
    version: 3.15.0
  - distribution: debian
    version: 11.3.0

aarch64:
  - distribution: alpine
    version: 3.15.0
  - distribution: debian
    version: 11.3.0

问题:
如何运行类似的命令如下:

- name: DENUG >>> db
    debug:
      var: db[item].alpine
    loop: "{{ db.keys()|list }}"
    when: db[item].alpine is defined  

并获得与输出中可用的相同结果。
基本上如何遍历这个新的 db.yml 来获取 db.x86_64.alpine.version 的值

ansible array
  • 1 个回答
  • 1174 Views
Martin Hope
knot22
Asked: 2022-04-01 12:09:31 +0800 CST

如何测试数组中是否存在索引

  • 5

我正在编写一个 Git Bash 实用程序,它将项目文件夹从一个位置复制到另一个位置。用户可能希望将项目复制到多个目标,但每次执行脚本只允许一个位置。这是迄今为止的逻辑 -

#!/bin/bash

# declare and initialize variables
source="/z/files/development/xampp/code/htdocs/Project7"

targets[0]="/z/files/development/xampp/code/htdocs/test/$(date +'%Y_%m_%d')"
targets[1]="/c/users/knot22/desktop/temp_dev/$(date +'%Y_%m_%d')"

# display contents of variables to user
echo "source " $source
echo -e "\nchoice \t target location"

for i in "${!targets[@]}"; do
  echo -e "$i \t ${targets[$i]}" 
done

echo

# prompt user for a target
read -p "Enter target's number for this copy operation: " target

到目前为止,一切都很好。接下来我想写一个if语句来检查用户输入的值是否target是targets. 在 PHP 中它会是array_key_exists($target, $targets). Bash 中的等价物是什么?

bash array
  • 2 个回答
  • 941 Views
Martin Hope
ajr_chm
Asked: 2022-03-23 02:05:35 +0800 CST

使用包含 {BEGIN FS = "|"} 的命令的 2 个单独文件中的 awk 匹配字段返回空行的输出

  • 1

两个文件:data1

    Name       |formula           |no. |dose|days|cost  |msg|em|notes
    Fname-Lname|BXXXT+GG          |8262|4   |14  |57.78 |   |  |sq
    Fname-Lname|SJXXT+GG          |8263|4¾  |14  |105.15|   |  |IB
    Fname-Lname|FJDHT+BH,LG,CQC,ZX|8264|5¾  |14  |46.20 |   |  |IB

数据2

10/12/2020|more-data-3456|105.15
10/12/2020|more-data-3456|95.10
11/12/2020|more.data-3456|30.30
14/12/2020|more-data-3456|45.55

我正在使用代码片段

awk 'BEGIN {FS = "|" } NR==FNR{a[$6];next} $3 in a {print $0}' data1 data2

为了匹配文件 data1 的 $6 中的值也出现在文件 data2 的 $3 中的位置。如果有匹配项,则打印出包含文件 data2 中匹配项的整个记录​​ ($0)。我期待:

10/12/2020|more-data-3456|105.15

但我只得到一个空行的输出。"|"我使用替换命令删除了文件分隔符" ",命令代码完全按预期工作,但我真的希望尽可能保留字段分隔符|。我想了解为什么添加 BEGIN 块会导致这种情况。它是否导致 awk 加载一个空数组而不是从 S6 获取数据?我的 awk 水平略高于初学者。编辑:我还使用了具有相同结果的 -F 参数,即空行的输出。我正在使用 gawk 。

awk array
  • 2 个回答
  • 143 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve