将两个文件的特定组件相乘并将答案放入另一个文件中

时间:2013-12-09 23:53:08

标签: bash shell

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 个答案:

答案 0 :(得分: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