2013-12-09 62 views
0
physical_entity#n#1:0.604068421516973, relation#n#1:2.48901579745054, 
group#n#1:2.0295442313808, communication#n#2:2.53726350891259, 
object#n#1:0.881796923419497, psychological_feature#n#1:1.5931115622059, 
measure#n#2:3.19027708303641, 

entity#n#1:100 physical_entity#n#1:83 abstraction#n#6:72 object#n#1:56 whole#n#2:53  
matter#n#3:41 causal_agent#n#1:41 psychological_feature#n#1:35 attribute#n#2:34 
living_thing#n#1:32 organism#n#1:32 event#n#1:28 state#n#2:28 substance#n#7:27 
artifact#n#1:27 person#n#1:26 act#n#2:25 relation#n#1:24 

兩個文件如上所示。我想乘以相應的類別,並將答案放在單獨的文件 中,方法是將冒號後面的值乘以每個類別。將兩個文件的特定組件相乘並將答案放置到另一個文件中

physical_entity#n#1:  50.1376789859 
psychological_feature#n#1 55.7589046772 

我會怎麼做呢?

回答

1

使用bash,bc,以及GNU的grep:

# read the first file 
declare -A a 
while IFS=: read -r cat value; do 
    a[$cat]=$value 
done < <(grep -oP '[\w#]+:[\d.]+' file1) 

# read the 2nd file and perform the multiplication 
declare -A b 
while IFS=: read -r cat value; do 
    [[ -n "${a[$cat]}" ]] && b[$cat]=$(bc -l <<< "${a[$cat]} * $value") 
done < <(grep -oP '[\w#]+:[\d.]+' file2) 

# output the result 
for key in "${!b[@]}"; do 
    printf "%s\t%s\n" "$key" "${b[$key]}" 
done > output 

結果

$ cat output 
psychological_feature#n#1 55.7589046772065 
relation#n#1 59.73637913881296 
physical_entity#n#1 50.137678985908759 
object#n#1 49.380627711491832 
相關問題