2015-04-08 46 views
0

一個不尋常的頭我有以下變數:sed的:加引號

STR1="CD45RA_naive" 
STR2="CD45RO_mem" 
STR3="CD127_Treg" 
STR4="IL17_Th_stim_MACS" 
STR5="IL17_Th17_stim" 
names=($STR1 $STR2 $STR3 $STR4 $STR5) 

和文件具有相同的名稱作爲一個「.bed」擴展變量。 每個文件,我需要添加以下標題:

track name='enhancer region' description='${names[$i]}'" 

我的想法是以下幾點:

for ((i=0; i<${#names[@]}; i++)) 
do 
    echo "output/annotation/"${names[$i]}".bed" 

    sed -i '' "1 i \track name='enhancer region' description='"${names[$i]}"'" "output/annotation/"${names[$i]}".bed" 

done 

不過,我得到一個錯誤:

sed: 1: "1 i \track name='enhanc ...": extra characters after \ at the end of i command 

什麼是正確的方法爲每個文件添加一個相應的頭文件(gawk,sed)?

+1

在''''幫助之後換行嗎? – choroba

+1

你使用Mac OS X還是BSD? – Wintermute

回答

0

您通常需要爲i命令插入的行自己行。換句話說,你需要在反斜槓後添加一個換行符。例如:

$ cat input 
line 1 
$ sed -e '1i\ 
foo' input 
foo 
line 1 
$ sed -e '1i\ foo' input 
sed: 1: "1i\ foo 
": extra characters after \ at the end of i command