简体   繁体   中英

Need to read a file and use results as named variables in a shell script

I have the following output file from a venn program.

[1, 2],106
[1, 3],2556
[2, 3],5207
[1, 2, 3],232
[2],7566
[3],8840
[1],5320

I need a command to get each line result number as variable argument in a script like this:

$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],

these results will then be used in the script below to draw a venn diagram.

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

In BASH it would be convenient to store the values in an associative array first and then assign them to the correct variables.

Suppose the output of venn is stored in venn.txt then the following code could parse it for you

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

Now you could extract the variables from the array in your script

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]"]}

In the parsing code we use Here String <<<word syntax in order to have the loop executed in the same shell instance as the rest of the code, so that the changes to the associative array were recorded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM