两个for循环Linux,带有两个输入文件 [英] Two for loops Linux with two input files

查看:328
本文介绍了两个for循环Linux,带有两个输入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个输入中有20个文件,在另一输入中有20个文件.我希望执行我的命令,该命令将从每个输入中同时获取两个文件并执行命令,即对于一个输入中的每个文件,它将从第二个输入获取相应的文件.这是我的尝试:

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

我希望循环运行此模式

推荐答案

现在是一个非常不同的问题!我首先监督,日期与零文件和avm文件不匹配.

A very different question now! And I've first overseen, that the dates don't match for zero- and avm-file.

您不需要2个循环,但需要一个映射. ${f1/zero-mam-2050-2074-/}提取类似-BNU.nc的内容,该内容也需要附加到 tasmax 文件以及您的 ydayadd 文件中.

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.

对于测试,如果您向我们提供了所有必要的信息,则上面的代码应该可以工作.为了真正完成工作,请将echo替换为cdo:

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

(还有另一个错误,因为我使用的是ext=${f/zer而不是ext=${f1/zer.)

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

测试:

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

这篇关于两个for循环Linux,带有两个输入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆