# Add label to all resourcesfind.-name"*.yaml"-execyq-i'.metadata.labels.environment = "production"'{}\;# Update image tag in all deploymentsyq-i'(.spec.template.spec.containers[].image | select(. == "*:latest")) |= sub(":latest", ":v1.2.3")'k8s/**/*.yaml
# Add annotation to specific resourcesyq-i'select(.kind == "Service") | .metadata.annotations."prometheus.io/scrape" = "true"'*.yaml
Testare sempre senza -iprima: Eseguire comandi senza il flag in-place per visualizzare in anteprima le modifiche prima di modificare i file
yq'.spec.replicas = 5'deployment.yaml# Preview firstyq-i'.spec.replicas = 5'deployment.yaml# Then apply
Utilizzare il controllo versione: Eseguire il commit dei file prima di modifiche in massa per poter facilmente annullare le modifiche se necessario
Racchiudere le espressioni tra virgolette: Utilizzare apici singoli per le espressioni per evitare l'interpretazione della shell
Note: Since some sections (3-20) were left blank in the original text, I've kept them blank in the translation as well. If you'd like me to fill those in with placeholder text or translations, please let me know.bash
yq '.items[] | select(.name == "test")' file.yaml # Correct- Convalida YAML dopo le modifiche: Assicurati che le tue modifiche producano YAML valido
yq'.'modified.yaml>/dev/null&&echo"Valid YAML"||echo"Invalid YAML"```-**Usa[terminemancante]**:Piùprecisorispettoall'aggiornare tutto`select()`[Testo incompleto]```bash yq '(select(.kind=="Deployment")|.spec.replicas)=3' file.yaml ```- **Preserva i commenti**: yq preserva i commenti di default, ma fai attenzione con trasformazioni complesse`eval-all`- **Usa [termine mancante] per operazioni su più file**: Più efficiente rispetto all'elaborazionedeifileseparatamente
```bash
yqea'. as $item ireduce ({}; . * $item)'*.yaml
```-**Sfruttalevariabilid'ambiente**: Mantieni i dati sensibili fuori dagli script```bash export SECRET_KEY="..." yq '.apiKey=env(SECRET_KEY)' config.yaml ```- **Usa output raw per gli script**: Usa [flag mancante] quando si invia a altri comandi`-r`[Testo incompleto]```bash IMAGE=$(yq -r '.spec.template.spec.containers[0].image' deployment.yaml) ```- **Controlla i codici di uscita per la convalida**: Usa [flag mancante] per fallire su risultati nulli/falsi`-e`[Testo incompleto]```bash yq -e '.spec.replicas>0' deployment.yaml && echo "Valid" || echo "Invalid" ```[Testo incompleto]## Risoluzione dei problemi| Problema | Soluzione ||-------|----------|| **Error: "bad file descriptor"** | Use `-i` flag correctly or redirect output: `yq '.' file.yaml > temp && mv temp file.yaml` || **Changes not persisted** | Add `-i` flag for in-place editing: `yq -i '.field="value"' file.yaml` || **"null" appears in output** | Field doesn'texistorisnull.Usealternativeoperator:`yq'.field // "default"'file.yaml`||**Commentsareremoved**|Use`...comments=""`toexplicitlyremove,orcheckifusingoperationsthatdon't preserve comments || **Array merge replaces instead of concatenates** | Use `*+` instead of `*` for merge: `yq ea 'select(fi==0)*+select(fi==1)' f1.yaml f2.yaml` || **"Error: bad expression"** | Controlla la sintassi dell'espressione,assicuratichelevirgolettesianocorrette,verificacheglioperatorisianocorretti||**Outputhasextraquotes**|Use`-r`flagforrawoutput:`yq-r'.name'file.yaml`||**Cannotprocessmultiplefiles**|Use`ea`(eval-all)command:`yqea'.'file1.yamlfile2.yaml`||**Wrongversionofyq**|Verifyyouhavemikefarah/yq(notkislyuk/yq):`yq--version`shouldshowgithub.com/mikefarah/yq||**Permissiondeniedon`-i`**|Ensurewritepermissions:`chmodu+wfile.yaml`orrunwithappropriateprivileges||**Encodingissueswithspecialcharacters**|EnsureUTF-8encoding:`yq--encoding=utf-8'.'file.yaml`||**Largefilescausememoryissues**|Processinchunksorusestreaming:`yq-N'.items[]'large-file.yaml`||**Pathnotfounderrors**|Verifypathexists:`yq'has("path.to.field")'file.yaml`beforeaccessing||**Mergeconflictswithcomplexstructures**|Useexplicitmergestrategies:`*d`fordeepmergewithdeletion,`*+`forarrayconcatenation|[Testoincompleto]## Riferimento rapido - Pattern comuniWouldyoulikemetoclarifyorcompletethemissingparts?```bash
# Read valueyq'.path.to.field'file.yaml
# Update valueyq-i'.path.to.field = "new-value"'file.yaml
# Delete fieldyq-i'del(.path.to.field)'file.yaml
# Filter arrayyq'.items[] | select(.name == "target")'file.yaml
# Merge filesyqea'select(fi==0) * select(fi==1)'base.yamloverride.yaml
# Convert formatyq-o=json'.'file.yaml
# Use environment variableyq'.field = env(VAR_NAME)'file.yaml
# Multiple operationsyq-i'.field1 = "value1" | .field2 = "value2"'file.yaml