我有一个包含以下内容的文件:
string1_204
string2_408
string35_592
Run Code Online (Sandbox Code Playgroud)
我需要摆脱string1_,string2_,string35_等等,并添加204,408,592来获取值.所以输出应该是1204.
我可以取出string1_和string 2_但是对于string35_592,我有5_592.我似乎无法让命令正确地做我想做的事情.请任何帮助表示赞赏:)
用awk:
awk -F_ '{s+=$2}END{print s}' your.txt
Run Code Online (Sandbox Code Playgroud)
输出:
1204
Run Code Online (Sandbox Code Playgroud)
说明:
-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
}
Run Code Online (Sandbox Code Playgroud)