2013-04-29 88 views
2

我在搜索/匹配源文件sourcefile.txt中的術語列表與我的目標文件target.bed中的術語列表。我想用相應的距離值將grep的術語打印到單獨的輸出文件中。在shell腳本中使用awk/grep搜索文本

源文件看起來是這樣的:

SMOX 
NCOA3 
EHF 

目標文件看起來是這樣的:

Chromosome PeakStart PeakEnd Distance GeneStart GeneEnd ClosestTSS_ID Symbol Strand 
chr20 4100204 4100378 -29134 4129425 4168394 SMOX null + 
chr20 6234586 46234754 -21075 46255745 46257534 NCOA3 null + 
chr11 34622044 34622238 -20498 34642639 34668098 EHF >null + 

輸出文件包含grep'd文本(ClosestTSS_ID和距離)

SMOX -29134 
NCOA -21075 
EHF -20498 

我試過這個腳本:

exec < sourcefile.txt 
while read line 
do 
genes=$(echo $line| awk '{print $1}') 
grep -w "genes" targetfile.bed | awk '{print $4,$7}' >> outputfile.txt 
done` 

但它不適用於我不同的源文件;我想要在同一個循環中包含許多不同的源文件,但該腳本僅適用於第一個。我使用了相同的腳本,但使用不同的文件名。

我已經試過這也:

rm sourcefile_temp.txt 
touch sourcefile_temp.txt 
awk 'NR>1{print $1}' sourcefile.txt > sourcefile_temp.txt 
exec < sourcefile_temp.txt 
while read line 
do 
set $line 
sourcefilevar=`grep $1 targetfile.bed| cut -f4| cut -f7` 
echo $line $tssmoq2 >> output.txt 
done` 

這一次給了我一個非常奇怪的輸出。

任何建議/更正/更好的方法來做到這一點將非常感激。

回答

2

awk腳本將做的工作:

$ awk 'FNR==NR{a[$1];next}FNR>1&&($7 in a){print $7,$4}' source target 
SMOX -29134 
NCOA3 -21075 
EHF -20498 
+2

謝謝你非常非常非常感謝! – user1879573 2013-04-29 15:40:42