2015-02-09 233 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 
相關問題