0

私の週末を台無しにした問題を紹介させてください。4列に生物学的データがあります

@ID:::12345/1 ACGACTACGA text !"#$%vwxyz  
@ID:::12345/2 TATGACGACTA text :;<=>?VWXYZ

awk を使用して最初の列を編集して文字を置き換え
たいと思います : and / with - 最後の列の文字列を、個々の ASCII 文字 (からアスキー 33 ~ 126)。

@ID---12345-1 ACGACTACGA text 33,34,35,36,37,118,119,120,121,122  
@ID---12345-2 TATGACGACTA text 58,59,60,61,62,63,86,87,88,89,90

最初の部分は簡単ですが、2 番目の部分で行き詰まっています。awk 序数関数と sprintf を使用してみました。前者は文字列の最初の文字でのみ機能し、後者はスペースではなく16進数を10進数に変換することしかできません。bash機能も試した

$ od -t d1 test3 | awk 'BEGIN{OFS=","}{i = $1; $1 = ""; print $0}' 

しかし、awk 内でこの関数を呼び出す方法がわかりません。awkでも実行できるダウンストリーム操作がいくつかあるため、awkを使用することをお勧めします。

よろしくお願いします

4

2 に答える 2

1

awk manualの序数関数を使用すると、次のように実行できます。

awk -f ord.awk  --source '{
    # replace : with - in the first field
    gsub(/:/,"-",$1)

    # calculate the ordinal by looping over the characters in the fourth field
    res=ord($4)
    for(i=2;i<=length($4);i++) {
        res=res","ord(substr($4,i))
    }
    $4=res
}1' file

出力:

@ID---12345/1 ACGACTACGA text 33,34,35,36,37,118,119,120,121,122
@ID---12345/2 TATGACGACTA text 58,59,60,61,62,63,86,87,88,89,90

ここにありますord.awkhttp://www.gnu.org/software/gawk/manual/html_node/Ordinal-Functions.htmlからそのまま取得)

# ord.awk --- do ord and chr

# Global identifiers:
#    _ord_:        numerical values indexed by characters
#    _ord_init:    function to initialize _ord_



BEGIN    { _ord_init() }

function _ord_init(    low, high, i, t)
{
    low = sprintf("%c", 7) # BEL is ascii 7
    if (low == "\a") {    # regular ascii
        low = 0
        high = 127
    } else if (sprintf("%c", 128 + 7) == "\a") {
        # ascii, mark parity
        low = 128
        high = 255
    } else {        # ebcdic(!)
        low = 0
        high = 255
    }

    for (i = low; i <= high; i++) {
        t = sprintf("%c", i)
        _ord_[t] = i
    }
}

function ord(str, c)
{
    # only first character is of interest
    c = substr(str, 1, 1)
    return _ord_[c]
}

function chr(c)
{
    # force c to be numeric by adding 0
    return sprintf("%c", c + 0)
}

の全体を含めたくない場合はord.awk、次のようにできます。

awk 'BEGIN{ _ord_init()}
function _ord_init(low, high, i, t)
{
    low = sprintf("%c", 7) # BEL is ascii 7
    if (low == "\a") {    # regular ascii
        low = 0
        high = 127
    } else if (sprintf("%c", 128 + 7) == "\a") {
        # ascii, mark parity
        low = 128
        high = 255
    } else {        # ebcdic(!)
        low = 0
        high = 255
    }

    for (i = low; i <= high; i++) {
        t = sprintf("%c", i)
        _ord_[t] = i
    }
}
{
    # replace : with - in the first field
    gsub(/:/,"-",$1)

    # calculate the ordinal by looping over the characters in the fourth field
    res=_ord_[substr($4,1,1)]
    for(i=2;i<=length($4);i++) {
        res=res","_ord_[substr($4,i,1)]
    }
    $4=res
}1' file
于 2013-07-22T11:42:53.693 に答える