我有一个venn程序的输出文件。
[1, 2],106
[1, 3],2556
[2, 3],5207
[1, 2, 3],232
[2],7566
[3],8840
[1],5320
在这样的脚本中,我需要一个命令将每个行号作为变量参数获取:
$area1=here it must come the results after [1],
$area2=here it must come the results after [2],
$area3=here it must come the results after [3],
$n12=here it must come the results after [1, 2],
$n13=here it must come the results after [1, 3],
$n23=here it must come the results after [2, 3],
$n123=here it must come the results after [1, 2, 3],
这些结果将在下面的脚本中用于绘制一个venn图。
cat << catfile >> $prefix1-VennDiagram.R
library(VennDiagram);
venn.diagram(
x = list(
"$sample1" = c(1:$area1, $(($area1+1)):$(($area1+$n12)), $(($area1+$n12+1)):$(($area1+$n12+$n123)), $(($area1+$n12+$n123+1)):$(($area1+$n12+$n123+$n13))),
"$sample2" = c($(($area1+$n12+$n123+$n13+1)):$(($area1+$n12+$n123+$n13+$area2)), $(($area1+1)):$(($area1+$n12)), $(($area1+$n12+1)):$(($area1+$n12+$n123)), $(($area1+$n12+$n123+$n13+$area2+1)):$(($area1+$n12+$n123+$n13+$area2+$n23))),
"$sample3" = c($(($area1+$n12+$n123+$n13+$area2+$n23+1)):$(($area1+$n12+$n123+$n13+$area2+$n23+$area3)), $(($area1+$n12+1)):$(($area1+$n12+$n123)), $(($area1+$n12+$n123+1)):$(($area1+$n12+$n123+$n13)), $(($area1+$n12+$n123+$n13+$area2+1)):$(($area1+$n12+$n123+$n13+$area2+$n23)))
),
filename = "$prefix1-VennDiagram.tiff",
col = "transparent",
fill = c("red", "blue", "green"),
alpha = 0.5,
label.col = c("darkred", "white", "darkblue", "white", "white", "white", "darkgreen"),
cex = 2.5,
fontfamily = "arial",
fontface = "bold",
cat.default.pos = "text",
cat.col = c("darkred", "darkblue", "darkgreen"),
cat.cex = 2.0,
cat.fontfamily = "arial",
cat.fontface = "italic",
cat.dist = c(0.06, 0.06, 0.03),
cat.pos = 0
);
catfile
Rscript $prefix1-VennDiagram.R
exit
最佳答案
在bash中,首先将值存储在关联数组中,然后将它们赋给正确的变量是很方便的。
假设venn
的输出存储在venn.txt
然后下面的代码可以为您解析它
declare -A venn
while read line ; do
key=$(echo "$line" | sed 's/^\(\[[^]]*\]\).*/\1/')
value=$(echo "$line" | sed 's/^\[[^]]*\],\(.*\)/\1/')
if [ -n "$key" ] ; then # This check is necessary
# to protect against empty lines
venn["$key"]="$value" ;
fi
done <<< "$( cat venn.txt )"
现在可以从脚本中的数组中提取变量
area1=${venn["[1]"]}
area2=${venn["[2]"]}
area3=${venn["[3]"]}
n12=${venn["[1, 2]"]}
n13=${venn["[1, 3]"]}
n23=${venn["[2, 3]"]}
n123=${venn["[1, 2, 3]"]}
在解析代码中,我们使用string
<<<word
语法,以便在与其余代码相同的shell实例中执行循环,从而记录对关联数组的更改。关于linux - 需要读取文件并将结果用作Shell脚本中的命名变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47713610/