Score:0

How to grab 2 data points from the beginning and end of a CSV file and subtract them using command line?

lk flag

I was able to script the following line to output just the column I needed. I can't seem to grab the first number, 56485.65, and the last, 56498.2, to subtract the two and output the results, though.

$ tail +7  2022-11-13.csv | sed "12 d"| cut  -d ";" -f 9
56485.65
56485.65
56485.65
56485.65
56485.65
56485.65
56485.65

56497.53
56497.72
56497.92
56497.99
56498.03
56498.07
56498.15
56498.2

Below is a shortened version of the CSV file. I cut out a chunk in the middle to shorten it up.

$ cat 2022-11-13.csv 
CSV-Export;Version: 1.01;Separator: Semicolon

;Emgncy03;Emgncy03;Emgncy03;WRHU0U5A;WRHU0U5A;WRHU0U5A;WRHU0U5A;WRHU0U5A;WRHU0U5A;WRHU0U5A;WRHU0U5A;WRHU0U5A;WRHU0U5A;WRHU0U5A;WRHU0U5A;WRHU0U5A;WRHU0U5A;WRHU0U5A;WRHU0U5A;WRHU0U5A;WRHU0U5A;WRHU0U5A;WRHU0U5A
;2120203458;2120203458;2120203458;2120048843;2120048843;2120048843;2120048843;2120048843;2120048843;2120048843;2120048843;2120048843;2120048843;2120048843;2120048843;2120048843;2120048843;2120048843;2120048843;2120048843;2120048843;2120048843;2120048843
TimeStamp;ErrCode1;ErrCode2;ErrCode3;A.Ms.Amp;A.Ms.Vol;A.Ms.Watt;Error;E-Total;GridMs.Hz;GridMs.PhV.phsA;GridMs.PhV.phsB;GridMs.PhV.phsC;Inv.TmpLimStt;MainModel;Mode;Mt.TotOpTmh;Mt.TotTmh;Op.EvtCntUsr;Op.EvtNo;Op.GriSwStt;Op.TmsRmg;Pac;Serial Number
hh:mm;;;;A;V;W;;kWh;Hz;V;V;V;;;;h;h;;;;s;W;
07:05;;;;0;352.26;0;0: -------;56485.65;59.97;245.92;0;0;1: NoneDrt;1: Solar-WR;7: Warten;42937.23;45042.04;432;0;1: Opn;0;0;2120048843
07:10;;;;0;370.58;0;0: -------;56485.65;59.98;244.48;0;0;1: NoneDrt;1: Solar-WR;7: Warten;42937.23;45042.13;432;0;1: Opn;0;0;2120048843
07:15;;;;0;399.37;0;0: -------;56485.65;59.99;244.09;0;0;1: NoneDrt;1: Solar-WR;7: Warten;42937.23;45042.21;432;0;1: Opn;0;0;2120048843
07:20;;;;0;426.34;0;0: -------;56485.65;59.99;245.07;0;0;1: NoneDrt;1: Solar-WR;7: Warten;42937.23;45042.29;432;0;1: Opn;0;0;2120048843
07:25;;;;0.06;394.03;21.97;0: -------;56485.65;59.99;245.34;0;0;1: NoneDrt;1: Solar-WR;1: MPP;42937.28;45042.37;432;0;2: Cls;0;12.86;2120048843
07:30;;;;0.12;360.71;41;0: -------;56485.65;59.99;245.56;0;0;1: NoneDrt;1: Solar-WR;1: MPP;42937.36;45042.46;432;0;2: Cls;0;26.55;2120048843
------  cut out chunk ----------
14:25;;;;6.3;403.19;2538.86;0: -------;56497.53;60;251.18;0;0;1: NoneDrt;1: Solar-WR;1: MPP;42944.28;45049.38;432;0;2: Cls;0;2431.1;2120048843
14:30;;;;6;405.64;2430.86;0: -------;56497.72;60;250.99;0;0;1: NoneDrt;1: Solar-WR;1: MPP;42944.36;45049.46;432;0;2: Cls;0;2327.17;2120048843
14:35;;;;6.35;385.46;2444.54;0: -------;56497.92;59.98;250.69;0;0;1: NoneDrt;1: Solar-WR;1: MPP;42944.45;45049.54;432;0;2: Cls;0;2337.07;2120048843
14:40;;;;2.35;391.32;928.54;0: -------;56497.99;59.99;247.68;0;0;1: NoneDrt;1: Solar-WR;1: MPP;42944.53;45049.63;432;0;2: Cls;0;873.39;2120048843
14:45;;;;1.3;400.13;519.54;0: -------;56498.03;59.99;247.57;0;0;1: NoneDrt;1: Solar-WR;1: MPP;42944.61;45049.71;432;0;2: Cls;0;489.62;2120048843
14:50;;;;1.19;402.03;478.04;0: -------;56498.07;60.02;247.29;0;0;1: NoneDrt;1: Solar-WR;1: MPP;42944.7;45049.79;432;0;2: Cls;0;450.39;2120048843
14:55;;;;2.28;419.46;956.9;0: -------;56498.15;60.01;248.64;0;0;1: NoneDrt;1: Solar-WR;1: MPP;42944.78;45049.88;432;0;2: Cls;0;914.59;2120048843
15:00;;;;1.46;408.06;593.76;0: -------;56498.2;59.99;247.76;0;0;1: NoneDrt;1: Solar-WR;1: MPP;42944.86;45049.96;432;0;2: Cls;0;562.93;2120048843
Score:1
ca flag

You can use awk to do that. You don't need to output all lines from line 7 to the end. You just need to get the value of the 9th column of line 7 and the 9th column of the last line and subtract them as follows:

awk -F ";" 'NR==7 {a=$9}; END {print $9 - a}' 2022-11-13.csv

The output is:

12.55

Explanation

  • -F ";": use ; as the field separator.

  • NR==7 {a=$9}: go to the 7th line (NR==7) and store the value of the 9th column ($9) to the variable a (a=$9).

  • END {print $9 - a}: subtract the value of variable a from the 9th column of the last line and print the result.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.