2015-02-09 168 views
0

正如通常:)我有两组名称相同但扩展名不同的输入文件。不同列表中的元素匹配

使用bash我做这创造2列出了具有相同元素的名称,而循环2个迪尔斯2套文件,并且只使用它的名字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)我需要在脚本的最后一步添加一些测试,只在元素相同的情况下创建最终的output.in文件(原则上在这种情况下它总是应该是相同的!)在这个操作中由printf匹配。

感谢您的帮助,

格列布

回答

0

下面是创建数组一个简单的方法:

# 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) 

要比较两个排序的数组,你可以使用join

# convenience function; could have helped above, too. 
lines() { printf %s\\n "[email protected]"; } 
# Some examples: 
# 1. compare a with b and print the lines which are only in a 
join -v 1 -t '' <(lines "${a[@]}") <(lines "${b[@]}") 
# 2. create c as an array with the lines which are only in b 
mapfile -t c < <(join -v 2 -t '' <(lines "${a[@]}") <(lines "${b[@]}")) 

如果创建两个差异表,那么这两个如果两个列表均为空,则数组相等。如果您希望两个阵列都是相同的,但是如果这对时间要求很高(可能不是),则可以进行简单的预检:

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