I have 20 files in one input and 20 files in another input. I wish to execute my command that will take two files from each input simultaneously and perform the command i.e. for each file in one input it will take the corresponding file from second input. Here is my attempt:
for f1 in zero-mam-2050-2074*.nc
do
for f2 in avm-mam-1976-2000-tasmax-*.nc
do
cdo ydayadd "$f1" "$f2" ydayadd-$file
done
done
I am writing the one line code that I wish will work in this loop. The syntax of code is
cdo ydayadd input_1 input_2 output
This code is running fine using one command at a time.
cdo ydayadd zero-mam-2050-2074-ACCESS.nc avm-mam-1976-2000-tasmax-ACCESS.nc ydayadd-ACCESS.nc
cdo ydayadd zero-mam-2050-2074-bcc.nc avm-mam-1976-2000-tasmax-bcc.nc ydayadd-bcc.nc
cdo ydayadd zero-mam-2050-2074-BNU.nc avm-mam-1976-2000-tasmax-BNU.nc ydayadd-BNU.nc
I wish to run this pattern in loop
I think the following script will show you one way to get what you want. This script echos the filename that are 'matched' up - change that command to be whatever you really want.
Note: this script assumes that you want the filenames "paired" based on the substring that matches the wildcard
"*"
character.It uses some bash variable replacement to strip off the parts of the filename in the first list of files that don't match the part of the name that matches the
"*"
wildcard (leaving the$wildcard
variable with the substring that matched the"*"
). It then uses that to construct the second filename in the pair. There's probably a better way to this operation, but I'm not a shell or bash expert.Hopefully if this doesn't quite do what you need, it'll give you some ideas:
A very different question now! And I've first overseen, that the dates don't match for zero- and avm-file.
You don't need 2 loops, but a mapping.
${f1/zero-mam-2050-2074-/}
extracts something like-BNU.nc
, which needs to appended to the tasmax file and to your ydayadd-file too.For testing, above code should work, if you gave us all neccessary information. For really performing the work, replace
echo
withcdo
:(There was another error, in that I used
ext=${f/zer
instead ofext=${f1/zer
.)Test: