2009-09-29 48 views
0

我有一個看起來像這樣的數據:接合線打破了現狀的SED

> sq1 
foofoofoobar 
foofoofoo 
> sq2 
quxquxquxbar 
quxquxquxbar 
quxx 
> sq3 
foofoofoobar 
foofoofoo 
> sq4 
foofoofoobar 
foofoo 

我想加入「> SQI」頭爲切斷線, 即高產的基礎上線:

foofoofoobarfoofoofoo 
quxquxquxbarquxquxquxbarquxx 
foofoofoobarfoofoofoo 
foofoofoobarfoofoo 

我試圖用這個sed但失敗:

sed '/^S/d;N;s/\n/\t/' 

有什麼正確的方法來做到這一點?

回答

3
#!/bin/sed -f 

# If this is a header line, empty it... 
s/^>.*// 
# ... and then jump to the 'end' label. 
t end 
# Otherwise, append this data line to the hold space. 
H 
# If this is not the last line, continue to the next line. 
$!d 
# Otherwise, this is the end of the file or the start of a header. 
: end 
# Call up the data lines we last saw (putting the empty line in the hold). 
x 
# If we haven't seen any data lines recently, continue to the next line. 
/^$/d 
# Otherwise, strip the newlines and print. 
s/\n//g 

# The one-line version: 
# sed -e 's/^>.*//;te' -e 'H;$!d;:e' -e 'x;/^$/d;s/\n//g' 
1

您正在測試行首的大寫字母「S」。您應該測試的大於號字符:

sed '/^>/d;N;s/\n/\t/' 

sed '/^> sq/d;N;s/\n/\t/' 

編輯:我錯過了一個事實,即有標題行之間的變量數。這是我到目前爲止有:

sed -n '/^>/{x; p; d}; /^>/!H; x; s/\n/\t/; h; $p' 

不幸的是,這留下的標題:

> sq1 foofoofoobar foofoofoo 
> sq2 quxquxquxbar quxquxquxbar quxx 
> sq3 foofoofoobar foofoofoo 
> sq4 foofoofoobar foofoo 

如果從Bash提示符做到這一點,你可能需要做set +H第一,所以你不要」由於感嘆號而導致歷史擴張干擾。

EDIT2:我的修訂版本,擺脫了頭:

sed -n '1{x;d};/^>/{x; p; d}; H; x; s/\n/\t/; s/^>.*\t//; h; $p' 
+0

@DW:您的片段似乎並沒有工作。我得到了「foofoofoobartfoofoofoo \ n quxquxquxbartquxquxquxbar \ n quxxt> sq3 \ n foofoofoobartfoofoofoo \ n foofoofoobartfoofoo」 – neversaint 2009-09-29 10:27:38

1

了原來的問題是bash溶液(即沒有 「頭」。):

#!/bin/bash 
text=[] 
i=0 

exec <$1 

while read line 
do 
    text[$i]=$line 
    let "i += 1" 
done 


j=0 
len=0 
while [ $j -lt ${#text[@]} ] 
do 
    string=${text[$j]} 
    if [ $len -le ${#string} ] ; then 
     printf $string 
    else 
     printf $string'\n' 
    fi 
    len=${#string} 
    let "j += 1" 
done 
printf '\n' 
1

我可以在sed中找不到一個簡單的方法。無論如何,與GAWK/mawk你只需要改變 的RS變量和削減換行符:

awk -v RS='> sq[0-9]' 'NR>1{gsub(/\n/,"");print}' file