Menu

Replace/edit character variable for multiple files

Help
2023-05-31
2023-06-01
  • Derrick Danso

    Derrick Danso - 2023-05-31

    Dear all,

    I am trying to replace the value of a character variable (say Times) in some multiple nc files.

    I can do that for one file like this:
    ncap2 -O -s 'Times(0,:)="2020-01-05_00:00:00";' file.nc file.nc

    I want to automate this over all the files, but each file has a different value for the Times variable. For each file, I get the value from the file name and store it in a variable (not in the nc file) say newval. Then I perform the operation above in a loop.

    I tried this but it didn’t work for me:

    newval="2020-01-06_00:00:00"                           # this is different for each file
    ncap2 -O -s 'Times(0,:)=$newval;' file.nc file.nc
    

    I would appreciate any help with this.

    Thanks
    Derrick

     
    • Charlie Zender

      Charlie Zender - 2023-05-31

      This appears to be a quoting issue. Try

      ncap2 -O -s "Times(0,:)=${newval}" file.nc file.nc
      
       
      • Derrick Danso

        Derrick Danso - 2023-05-31

        Thank you for your response!
        I tried and got the following error:

        Command-line script:1:22: unexpected token: _00

         
        • Charlie Zender

          Charlie Zender - 2023-06-01

          Be persistent, and notice the clues in the error message. Try

          ncap2 -O -s "Times(0,:)=\"${newval}\"" file.nc file.nc

           
          • Derrick Danso

            Derrick Danso - 2023-06-01

            This works! I appreciate your help.

             

Log in to post a comment.

MongoDB Logo MongoDB