Jahangir Ali
Jahangir Ali

Reputation: 9

Two for loops Linux with two input files

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

Upvotes: 0

Views: 228

Answers (2)

user unknown
user unknown

Reputation: 36229

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 with cdo:

for f1 in zero-mam-2050-2074-*.nc
do
   ext=${f1/zero-mam-2050-2074-/}
   f2=avm-mam-1976-2000-tasmax-${ext}
   cdo ydayadd "$f1" "$f2" ydayadd-$ext
done

(There was another error, in that I used ext=${f/zer instead of ext=${f1/zer.)

Test:

ls -1
avm-mam-1976-2000-tasmax-ACCESS.nc
avm-mam-1976-2000-tasmax-bcc.nc
avm-mam-1976-2000-tasmax-BNU.nc
zero-mam-2050-2074-ACCESS.nc
zero-mam-2050-2074-bcc.nc
zero-mam-2050-2074-BNU.nc

alias cdo=echo 
for f1 in zero-mam-2050-2074-*.nc
> do
>    ext=${f1/zero-mam-2050-2074-/}
>    f2=avm-mam-1976-2000-tasmax-${ext}
>    cdo ydayadd "$f1" "$f2" ydayadd-$ext
> done
ydayadd zero-mam-2050-2074-ACCESS.nc avm-mam-1976-2000-tasmax-ACCESS.nc ydayadd-ACCESS.nc
ydayadd zero-mam-2050-2074-BNU.nc avm-mam-1976-2000-tasmax-BNU.nc ydayadd-BNU.nc
ydayadd zero-mam-2050-2074-bcc.nc avm-mam-1976-2000-tasmax-bcc.nc ydayadd-bcc.nc

Upvotes: 1

Michael Burr
Michael Burr

Reputation: 340188

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:

for f1 in zero-mam-2050-2074*.nc
    do 

    # remove the part of the filename from before the "*" wildcard
    wildcard=${f1#zero-mam-2050-2074}

    #remove the part of the filename from after the "*" wildcard
    wildcard=${wildcard%.nc}

    # create the second filename
    f2=avm-mam-1976-2000-tasmax-${wildcard}.nc
    echo "$f1" "$f2"
done

Upvotes: 0

Related Questions