1

次の内容のファイルがあります。

string1_204
string2_408
string35_592

string1_、string2_、string35_ などを取り除き、204,408,592 を追加して値を取得する必要があります。したがって、出力は 1204 になります。

string1_ と string 2_ を取り出すことができますが、string35_592 には 5_592 があります。自分がやりたいことをするためのコマンドを正しく取得できないようです。どんな助けでも大歓迎です:)

4

2 に答える 2

5

awk を使用:

awk -F_ '{s+=$2}END{print s}' your.txt 

出力:

1204

説明:

-F_    sets the field separator to _ what makes it easy to access
       the numbers later on

{
    # runs on every line of the input file
    # adds the value of the second field - the number - to s.
    # awk auto initializes s with 0 on it's first usage
    s+=$2
}
END {
    # runs after all input has been processed
    # prints the sum
    print s
}
于 2013-08-23T03:08:50.040 に答える