简体   繁体   中英

sed cannot find file

I'm using sed in a bash script to change some variable values. 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. 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.

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. Sed is looking for a file that is literally named "$NAMELIST" instead of say "/the/path/to/namelist.txt"...

Check whatever logic is setting the NAMELIST variable.

Another explanation is that NAMELIST literally contains the phrase "can't read ". To whit:

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

How are you setting NAMELIST ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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