简体   繁体   English

sed找不到文件

[英]sed cannot find file

I'm using sed in a bash script to change some variable values. 我在bash脚本中使用sed来更改一些变量值。 These variables are mixture of different types and include also some special characters: 这些变量是不同类型的混合,还包括一些特殊字符:

 sed -i -e "s/NPROC=[^,]*,/NPROC=$NPROC/" \
      -e "s/NFRPOS=[^,]*,/NFRPOS=$OUTPUTFREQ/" \
      -e "s/NFRHIS=[^,]*,/NFRHIS=$OUTPUTFREQ/" \
      -e "s/COSP_OUTPUT_FREQUENCY=[^,]*,/COSP_OUTPUT_FREQUENCY=$OUTPUTFREQ/" \
      -e "s/TSTEP=[^,]*,/TSTEP=$TSTEP/" \
      -e "s/NCEXTR=[^,]*,/NCEXTR=$NCEXTR/" \
      -e "s/NVXTR2=[^,]*,/NVXTR2=$NVXTR2/" \
      -e "s/NVEXTR=[^,]*,/NVEXTR=$NVEXTR/" \
      -e "s/COSP_NLEVELS=[^,]*,/COSP_NLEVELS=$NLEV/" \
      -e "s/NVEXTRAGB=[^,]*,/$NVEXTRAGB/" \
      -e "s/NVEXTR2GB=[^,]*,/$NVEXTR2GB/" \
      -e "s/NFPLEV=[^,]*,/NFPLEV=$NFPLEV/" \
      -e "s/CNMEXP=[^,]*,/CNMEXP=\"${EXPID}\"/" \
      -e "s/LFPOS=[^,]*,/NFPOS=2/" \
      -e "s/NLAT=[^,]*,/NLAT=$NLAT/" \
      -e "s/NLON=[^,]*,/NLON=$NLON/" \
         $NAMELIST


+ sed -i -e 's/NPROC=[^,]*,/NPROC=10/' ' '
sed: can't read  : No such file or directory

However, I keep getting the error that the file $NAMELIST cannot be found. 但是,我不断收到错误消息,找不到文件$ NAMELIST。 The file does exits in the same directory and there is no error in the name. 该文件确实在同一目录中退出,并且名称没有错误。 Adding the fullpath doesn't help either. 添加全路径也无济于事。 So I'm wondering what is wrong in this sed command. 所以我想知道这个sed命令出了什么问题。

You have a space after the first backslash. 第一个反斜杠后有一个空格。 Instead of escaping the newline to continue the command, you're escaping the space, so it thinks the name of the file to edit is a single space. 您不是转义换行来继续该命令,而是转义了空格,因此它认为要编辑的文件名是单个空格。

Make sure that the backslashes are the last character on every line. 确保反斜杠是每一行的最后一个字符。

For whatever reason your NAMELIST variable is not being expanded. 无论出于何种原因,您的NAMELIST变量都不会被扩展。 Sed is looking for a file that is literally named "$NAMELIST" instead of say "/the/path/to/namelist.txt"... Sed正在寻找一个文件名为“ $ NAMELIST”的文件,而不是“ /the/path/to/namelist.txt” ...

Check whatever logic is setting the NAMELIST variable. 检查设置NAMELIST变量的任何逻辑。

Another explanation is that NAMELIST literally contains the phrase "can't read ". 另一个解释是, NAMELIST确实包含短语“无法读取”。 To whit: 白衣:

$ NAMELIST="can't read "
$ sed -e 1d "$NAMELIST"
sed: can't read : No such file or directory

How are you setting NAMELIST ? 您如何设置NAMELIST

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

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