bash - Read columns from a file into variables and use for substitute values in another file -
i have following file : input.txt
b73_chr10 w22_chr9 w22_chr7 w22_chr10 w22_chr8 w22_chr8
i have written following code(given below) read first , second column , substitute values of first column values in second column in output.conf file .for example, change value b73_chr10 w22_chr9,w22_chr7 w22_chr10,w22_chr8 w22_chr8 , keep doing values till end.
value1=$(echo $line| awk -f\ '{print $1}' input.txt) value2=$(echo $line| awk -f\ '{print $2}' input.txt) sed -i '.bak' 's/$value1/$value2/g' output.conf cat output.conf
output.conf
<rules> <rule> condition =between(b73_chr10,w22_chr1) color = ylgn-9-seq-7 flow=continue z=9 </rule> <rule> condition =between(w22_chr7,w22_chr2) color = blue flow=continue z=10 </rule> <rule> condition =between(w22_chr8,w22_chr3) color = vvdblue flow=continue z=11 </rule> </rules>
i tried commands(as above),but leaving blank file me.can guide went wrong ?
i suspect sed
wrong tool this. can you're asking in bash alone:
#!/usr/bin/env bash # declare associative array (requires bash 4) declare -a repl=() # step through our replacement file, recording array. while read that; repl["$this"]="$that" done < inp1 # read input file, replacing things strings noted in array. while read line; string in "${!repl[@]}"; line="${line/$string/${repl[$string]}}" done echo "$line" done < circos.conf
this approach of course oversimplified , therefore shouldn't used verbatim -- you'll want make sure you're editing lines want edit (verifying match /condition =between/
example). note because solution uses associative array (declare -a ...
), depends on bash version 4.
if solve awk, same basic principle apply:
#!/usr/bin/awk -f # collect tranlations first file. nr==fnr { repl[$1]=$2; next } # step through input file, replacing required. { ( string in repl ) { sub(string, repl[string]) } } # , print. 1
you'd run first argument being translation file, , second being input file:
$ ./thisscript translations.txt circos.conf
Comments
Post a Comment