Skip to main content
21 events
when toggle format what by license comment
Jan 1, 2020 at 17:54 comment added genip26057 i have created a another solution like your solution see the below answer ... suggest any improvement needed in script ... this is working as expected scenario which i am work environment
Jan 1, 2020 at 7:45 history edited Kusalananda CC BY-SA 4.0
added 107 characters in body
Jan 1, 2020 at 7:45 vote accept genip26057
Jan 1, 2020 at 7:40 comment added genip26057 i have new idea is it possible to implement ... as the script requires column names for which sum need to be calculate what if we can store the position of column name like SAL comes in position $3 and COST = $4 and $5 , $6 and so on. by using looping passing $3 $4 $5 $6 for this code awk -F'|' '{T+=$3} END { printf "%.10f\n", T }' demo.txt ... inplace or $3 it will go on replacing values first time it will be $3 then $4 ... $5 $6
Jan 1, 2020 at 7:35 comment added Kusalananda @genip26057 Calculating the sum over a single column is a different problem from calculating the sum over a variable number of columns. Imagine someone with terabytes of data. They do not want to parse the file six times just to sum over six columns.
Jan 1, 2020 at 7:34 comment added genip26057 can you refer solution of markgraf : unix.stackexchange.com/questions/558377/… in this code only decimal should be able to calculate ... your code is awsome then him ... but our environment work does not allow new file to create. only in one file i need to implement this all
Jan 1, 2020 at 7:33 comment added Kusalananda @genip26057 Sorry, you're obviously ignoring what I'm writing. I'm not answering more follow-up questions.
Jan 1, 2020 at 7:33 history edited Kusalananda CC BY-SA 4.0
added 101 characters in body
Jan 1, 2020 at 7:32 comment added genip26057 Ok i am happy with your code you created another script ...is it possible to do in one script only like this code : #!/bin/bash FILE="$1" COLUMNS="${@:2}" for col in $COLUMNS; do colnum=$(awk -v RS='|' '/'$col'/{ print NR; exit}' $FILE) awk '{FS="|"}{s+='$colnum'}END{print "'$col' ", s}' $FILE done | column -t
Jan 1, 2020 at 7:29 comment added Kusalananda @genip26057 That is what my solution does. But with the column names being |-delimited.
Jan 1, 2020 at 7:29 comment added genip26057 this is how the code is "bash scriptname.sh filename.txt SAL COST PER TAG" it should automatically take the column name and sum the particular column name for SAL COST PER TAG
Jan 1, 2020 at 7:27 comment added Kusalananda @genip26057 Yes. You do not want to have to parse your file once for each column though, surely?
Jan 1, 2020 at 7:26 comment added genip26057 sum of entire column SAL COST PER TAG : run this command you will get to see the output for SAL : awk -F'|' '{T+=$3} END { printf "%.10f\n", T }' demo.txt same for COST PER TAG
Jan 1, 2020 at 7:23 comment added Kusalananda @genip26057 Note that you have not indicated in the slightest way what you expect that a user give as arguments to the script. In the first coed it seems to be column names, in the second, it's a (single) column number?
Jan 1, 2020 at 7:21 comment added genip26057 @can you modify the provide code to make it work ...pls
Jan 1, 2020 at 7:21 comment added Kusalananda @genip26057 Also see unix.stackexchange.com/questions/50044/…
Jan 1, 2020 at 7:20 comment added Kusalananda @genip26057 You're using $col in the awk code, but this would refer to column number col, and there's no col variable in the program. Therefore, $col is the same as $0, which is the complete line.
Jan 1, 2020 at 7:18 history edited Kusalananda CC BY-SA 4.0
added 97 characters in body
Jan 1, 2020 at 7:15 comment added genip26057 can you tell me how to make my script work i have updated the question ... added newly created code at the end ... where as your solution is really good .... is it possible to do with my code with liitle chnages pls look question updated
Jan 1, 2020 at 7:10 history edited Kusalananda CC BY-SA 4.0
added 604 characters in body
Jan 1, 2020 at 7:04 history answered Kusalananda CC BY-SA 4.0