Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 不同列表中的元素匹配_Bash_List - Fatal编程技术网

Bash 不同列表中的元素匹配

Bash 不同列表中的元素匹配,bash,list,Bash,List,通常:我有两组输入文件,它们的名称相同,但扩展名不同 使用bash,我制作了一个简单的脚本,它创建了两个具有相同元素名称的列表,同时从两个dir循环两组文件,并仅使用w/o扩展名作为这些列表的元素: #!/bin/bash workdir=/data2/Gleb/TEST/claire+7d4+md/water_analysis/MD1 traj_all=${workdir}/tr_all top_all=${workdir}/top_all #make 2 lists for both fi

通常:我有两组输入文件,它们的名称相同,但扩展名不同

使用bash,我制作了一个简单的脚本,它创建了两个具有相同元素名称的列表,同时从两个dir循环两组文件,并仅使用w/o扩展名作为这些列表的元素:

#!/bin/bash
workdir=/data2/Gleb/TEST/claire+7d4+md/water_analysis/MD1
traj_all=${workdir}/tr_all
top_all=${workdir}/top_all

#make 2 lists for both file types
Trajectories=('');
Topologies=('');

#looping of 1st input files
echo "Trr has been found in  ${traj_all}:"
for tr in ${traj_all}/*; do # ????
 tr_n_full=$(basename "${tr}")
 tr_n="${tr_n_full%.*}"
 Trajectories=("${Trajectories[@]}" "${tr_n}");
done
#sort elements within ${Trajectories[@]} lists!!  >> HERE I NEED HELP!

#looping of 2nd files
echo "Top has been found in ${top_all}:"
for top in ${top_all}/*; do # ????
 top_n_full=$(basename "${top}")
 top_n="${top_n_full%.*}"
 Topologies=("${Topologies[@]}" "${top_n}");
done
#sort elements within ${Topologies[@] lists!!  >> HERE I NEED HELP!


#make input.in file for some program- matching of elements from both lists  >> HERE I NEED HELP!
for i in $(seq 1 ${#Topologies[@]}); do
printf "parm $top_all/${Topologies[i]}.top \ntrajin $traj_all/${Trajectories[i]}.mdcrd\nwatershell ${Area} ${output}/watershell_${Topologies[i]}_${Area}.dat > output.in
done
如果有人能为我提供如何改进此脚本的良好可能性,我将不胜感激: 1我需要在每个列表中添加最后一个元素后,以类似的模式对两个列表中的元素进行排序; 2我需要在脚本的最后一步添加一些测试,这将创建最终输出。在文件中,只有在元素原则上相同的情况下,在这种情况下,它始终应该是相同的!在这两个列表中,printf在此操作期间匹配

谢谢你的帮助


Gleb

以下是创建阵列的一种更简单的方法:

# Create an empty array
Trajectories=();

for tr in "${traj_all}"/*; do
  # Remove the string of directories
  tr_base=${tr##*/}
  # Append the name without extension to the array
  Trajectories+="${tr_base%.*}"
done
在bash中,这通常会导致一个排序列表,因为glob中*的扩展是排序的。但你可以用sort排序;如果您确定文件名中没有换行符,则最简单的方法是:

mapfile -t sorted_traj < <(printf %s\\n "${Trajectories[@]}" | sort)
如果创建两个差异列表,则如果两个列表均为空,则两个数组相等。但是,如果您希望两个阵列相同,并且这可能不是时间关键性的,则可以执行简单的预检查:

if [[ "${a[*]}" = "${b[*]}" ]]; then
  # the arrays are the same
else
  # the arrays differ; do some more work to see how.
fi
if [[ "${a[*]}" = "${b[*]}" ]]; then
  # the arrays are the same
else
  # the arrays differ; do some more work to see how.
fi