sách gpt4 ăn đã đi

Tham gia sau khi tham gia với yq (tham gia sau khi tham gia với yq)

In lại Tác giả: trợ lý lỗi Thời gian cập nhật: 25-10-2023 13:41:45 33 4
mua khóa gpt4 giày nike



I have the following structure in yml:

我在YML中有以下结构:


siêu dữ liệu:
name: nws-bot
version: 0.1.0
việc làm:
- name: name1
script: job1.sh
schedule: 0 20 * * *
- name: name2
script: job2.sh
schedule: 0 22 * * *

I'm trying to create a structure like this:

我正试着创造一个这样的结构:


name1~job1.sh;name2~job2.sh

The job name and script are separated by a ~, and the jobs are separated by a ;.

作业名称和脚本用~分隔,作业用;分隔。


I can get this to work using

我可以使用以下命令使其正常工作


yq '.metadata.jobs[] | [[.name, .script] | join("~")]' project.yml | yq '. | join(";")'

However I wonder if it can be done with a single yqexpression, instead of piping to yq a second time.

然而,我想知道是否可以用一个yq表达式来完成,而不是通过管道再次使用yq。


Thêm câu trả lời
Khuyến nghị câu trả lời tuyệt vời

Iterating with .metadata.jobs[] (which is a shortcut for .metadata.jobs | .[]) also destructures the array into its individual items. To re-capture them (meanwhile modified with the first tham gia) back into an array (to be applied to the second tham gia) necessitates the collecting array to be superordinate to the destructuring. Thus, move the iteration inside of it, i.e. pull the opening bracket up front (with the initial traversal to .metadata.jobs included or excluded at your preference):

使用.metadata.Jobs[](.metadata.Jobs|.[]的快捷方式)进行迭代也会将数组分解为其单独的项。要将它们重新捕获(同时使用第一个联接进行修改)回到一个数组中(应用于第二个联接),收集数组必须高于析构。因此,将迭代移到其中,即将左方括号拉到前面(根据您的喜好包括或排除对.metadata.Jobs的初始遍历):


yq '.metadata.jobs[] | [[.name, .script] | join("~")]' project.yml | yq 'join(";")'
# ╭──────────────────╯
yq '[.metadata.jobs[] | [.name, .script] | join("~")] | join(";")' project.yml
# ╰────────────────╮
yq '.metadata.jobs | [.[] | [.name, .script] | join("~")] | join(";")' project.yml

Actually, having [.[] | …] is what map(…) does: applying a filter to the items of an array while retaining the array structure. Using named filters can make code more descriptive and facilitate its readability. Alternatively, there's also the update operator |= which can be used to alter the items of a (generally deeper) structure without destructuring it. In both cases, however, your context here needs to be the array itself, so the initial traversal to .metadata.jobs must be completed prior to their usage (e.g. .metadata.jobs[] |= … would in excess also retain the top level objects, making the second tham gia eventually fail).

Trên thực tế, có [[]|[...] là những gì map(...) thực hiện: áp dụng bộ lọc cho các mục của mảng trong khi vẫn giữ nguyên cấu trúc mảng. Việc sử dụng các bộ lọc được đặt tên có thể làm cho mã của bạn mang tính mô tả hơn và cải thiện khả năng đọc của nó. Ngoài ra, còn có toán tử cập nhật |= có thể được sử dụng để thay đổi một mục có cấu trúc (thường sâu hơn) mà không phá hủy nó. Tuy nhiên, trong cả hai trường hợp, ngữ cảnh của bạn cần phải là chính mảng đó, do đó, siêu dữ liệu.jobs phải hoàn thành quá trình duyệt ban đầu của .www.example.com trước khi sử dụng chúng (ví dụ: .metadata.jobs[]|=.cũng sẽ giữ lại phần trên cùng -level đối tượng nhiều đến mức kết nối thứ hai cuối cùng không thành công).


yq '.metadata.jobs | map([.name, .script] | join("~")) | join(";")' project.yml
# or
yq '.metadata.jobs | .[] |= ([.name, .script] | join("~")) | join(";")' project.yml

Given your sample input, all of them output:

给出您的样例输入,所有它们都输出:


name1~job1.sh;name2~job2.sh

Thêm câu trả lời
33 4 0
trợ lý lỗi
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress