發表文章

目前顯示的是 11月 29, 2018的文章

How do you calculate sum of a column without creating temp files in shell script?

圖片
up vote 1 down vote favorite given an input file with data like this: id,name 10,abc 20,xyz 30,def I am trying to sum the values in the id column. Note: the reason I am using awk -v header is because I have multiple files all of which have id as a common header name but at different positions. The output I am expecting is the total sum of id (i.e., for the example above the output is 60). The code below works and returns the expected output, but I have to create a temp file in the code and then calculate the sum. I tried many variations unfortunately all my efforts failed. I want to avoid writing the data to a temp file, intfile.txt, but I am stuck. Any solutions/suggestions appreciated. ps: I am relatively new to shell scripting and I know code is not written well, but I am working through it. #!/bin/bash a...