我有几个具有以下命名样式的表:
INSTITUTE_MODEL_SCENARIO_RIVER.txt
例如:
wbm_gfdl_rcp8p5_mississippi.txt
wbm_hadgem_rcp8p5_mississippi.txt
wbm_miroc_rcp8p5_mississippi.txt
matsiro_gfdl_rcp4p5_mississippi.txt
matsiro_ipsl_rcp8p5_mississippi.txt
INSTITUTE
我想构建一个输出表,将文件名中包含和 的名称的项分组SCENARIO
。为此,我有以下循环:
for institute in wbm matsiro
do
for scenario in rcp4p5 rcp8p5
do
paste "$institute"_*_"$scenario"* > "$hyd_model"_"$scenario".txt
done
done
不幸的是,在将表格附加到第一个表格时,我想删除前三列(以避免冗余)。我知道在命令行中执行此操作:
paste wbm_gfdl_rcp8p5_mississippi.txt <(cut -f4 wbm_hadgem_rcp8p5_mississippi.txt) <(cut -f4 wbm_miroc_rcp8p5_mississippi.txt) > output.txt
但我没有成功在循环中实现这一点for
。欢迎提供任何提示!
答案1
有趣的问题。我将使用数组来捕获相关文件,并使用递归函数粘贴每个附加文件:
paste_files() {
if [[ "$#" -eq 0 ]]; then
cat -
else
paste - <(cut -f4 "$1") | paste_files "${@:1}"
fi
}
for institute in wbm matsiro; do
for scenario in rcp4p5 rcp8p5; do
files=( "$institute"_*_"$scenario"* )
cat "${files[0]}" | paste_files "${files[@]:1}" > "$hyd_model"_"$scenario".txt
done
done