简体   繁体   English

尝试替换行时,如果“sed:无输入文件”出现 sed 错误

[英]Sed error in if "sed: no input files" when try replace lines

I try change replicas count in yaml files .我尝试更改 yaml 文件中的副本数。 My script find need me information and must read text from keyboard but he skip read and send me "sed: no input files".我的脚本发现需要我的信息并且必须从键盘读取文本但他跳过阅读并向我发送“sed:无输入文件”。 What I do wrong ?我做错了什么? Thank you for attention感谢您的关注

#!/bin/bash
FILES=./test1/*

for file in $FILES
do
    echo "$file"
    grep -v -e "commonconfigs" -e "commonsecrets" -e "internalurls" $file | grep -w "[^-] name:\|replicas:"
    while IFS= read line
    do
    currentline=$(echo "$line" | grep -o "replicas")
      if  [ "$currentline" == "replicas" ];then
        read -p "replics:" rep
        sed -i 's/replicas:.*/replicas:$rep/g' > "$line"
      fi
    done < "$file"
done

Output:输出:

./test1/anal.yaml
  name: anal
  replicas: 1
  name: anal
template:
sed: no input files

Example file示例文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: devices-api
spec:
  selector:
  replicas: 1
  template:
    metadata:
        fluentd: "true"
      annotations:
        prometheus.io/scrape: 'true'
    spec:
      containers:
      - name: devices-api
        image:
        imagePullPolicy: "Always"
        resources: {}
        env:
          - name: Swagger__Enabled
            valueFrom:
              configMapKeyRef:
                name: commonconfigs
---

apiVersion: apps/v1
kind: Deployment
metadata:
  selector:
      app: event-jobs
  replicas: 1
  template:

Next statement of your code is guilty:你的代码的下一个语句是有罪的:

do [...] sed -i 's/replicas:.*/replicas:$rep/g' > "$line" [...] done < "$file"

The sed command called with the option -i is used to edit the file in place, which is not possible if you read the file from the standard input (outside of the do-while loop).使用选项-i调用的sed命令用于就地编辑文件,如果您从标准输入(在 do-while 循环之外)读取文件,则这是不可能的。

If your goal is to replace replicas: in yaml files then there's no need to use while or grep .如果您的目标是替换replicas:在 yaml 文件中,则无需使用whilegrep Use this sed command:使用这个sed命令:

sed "s/\<replicas:.*/replicas: $rep/g" test1/*yaml

Double instead of single quotes allow for parameter expansion of $rep .双引号而不是单引号允许$rep参数扩展。 Test it first, if output looks good use sed -i'' option to replace the files.首先测试它,如果输出看起来不错,请使用 sed -i''选项替换文件。

Thanks all for answers .谢谢大家的回答。 My problem will be at my algorithm parsing .我的问题将出在我的算法解析上。 I try read every line and set them in sed .我尝试阅读每一行并将它们设置在 sed 中。

sed -i "$32 s|replicas:.*|replicas: $rep|" $file

Now I found number of line with grep and set them in array and set there in sed where i need change something现在我找到了 grep 的行数并将它们设置在数组中并设置在 sed 中我需要更改某些内容的位置

Solution of parsing my files :解析我的文件的解决方案:

#!/bin/bash
FILES=*yaml

for file in $FILES
do
    echo $file
    grep -v -e "commonconfigs" $file | grep -w "[^-] name:\|replicas:"
    nummeric=($(grep -n "replicas" $file | cut -f1 -d:))
    for i in "${nummeric[@]}"; do
      echo $i
      read -p "replicas: " rep
      sed -i "$i s|replicas:.*|replicas: $rep|" $file
    done
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM