FWIW, I think that sorting early makes things easier. You can use commands like head
to keep only those above the line and wc
to determine where that line is. As it’s kind of awkward to work on data when you don’t know what it looks like, I’ve used a small table as data for this (see below). The script requires a csv on the desktop named ‘passing.csv’.
The script will run wc
on the data to determine how many lines it has and uses this to calculate the top 25%. Of course, you could also just arbitrarily insert a number.
Next, it removes header and footer rows, sorts the data based on column 5 in descending order, keeps only the top 25% and then outputs to top
, which it creates on the desktop.
It then retrieves the header row from the source data and finally concatenates the header row and top data into final
. If your data doesn’t have headers or footers, adjust accordingly.
set csv to quoted form of (POSIX path of (path to desktop as text) & "passing.csv")
set h2 to quoted form of (POSIX path of (path to desktop as text) & "header2.csv")
set top to quoted form of (POSIX path of (path to desktop as text) & "top.csv")
set final to quoted form of (POSIX path of (path to desktop as text) & "final.csv")
-- calculate top 25%
set counts to do shell script "wc " & csv & " | tr -s ' '"
set AppleScript's text item delimiters to space
set lineCounts to ((text item 2 of counts as integer) - 5) / 4 as integer -- minus for headers and footers
-- remove headers,footers, sort on column 5, reverse order, keep top 25%, output to top
do shell script "cat " & csv & " | head -n 34 | tail -n 32 | sort -t, -k5,5 -gr | head -n " & lineCounts & " > " & top
-- extract header from csv to h2
set head2 to do shell script "cat " & csv & " | head -n 2 | tail -n 1" & " > " & h2
-- concatenate h2 & top to final
do shell script "cat " & h2 & space & top & " > " & final
You could add something like this to the main line to replace commas with tabs:
sed 's/,/ /g'
The guts of it as a shell command would be this:
cat passing.csv | head -n 34 | tail -n 32 | sort -t, --key=5,5 -gr | head -n8
The output will be the top eight teams in total yardage.
Source data:
",,,,,Tot Yds & TO,Tot Yds & TO
Rk,Tm,G,PF,Yds,Ply,Y/P
1,Kansas City Chiefs,17,496,7032,1094,6.4
2,Philadelphia Eagles,17,477,6614,1124,5.9
3,Dallas Cowboys,17,467,6034,1114,5.4
4,Buffalo Bills,16,455,6361,1037,6.1
5,Detroit Lions,17,453,6460,1092,5.9
6,San Francisco 49ers,17,450,6216,1047,5.9
7,Minnesota Vikings,17,424,6145,1123,5.5
8,Cincinnati Bengals,16,418,5768,1053,5.5
9,Seattle Seahawks,17,407,5976,1044,5.7
10,Jacksonville Jaguars,17,404,6075,1072,5.7
11,Miami Dolphins,17,397,6197,1009,6.1
12,Las Vegas Raiders,17,395,5993,1049,5.7
13,Los Angeles Chargers,17,391,6108,1154,5.3
14,Green Bay Packers,17,370,5745,1051,5.5
15,Atlanta Falcons,17,365,5417,1011,5.4
16,New York Giants,17,365,5676,1089,5.2
17,New England Patriots,17,364,5348,1006,5.3
18,Cleveland Browns,17,361,5934,1116,5.3
19,Baltimore Ravens,17,350,5760,1052,5.5
20,Carolina Panthers,17,347,5206,976,5.3
21,Arizona Cardinals,17,340,5499,1144,4.8
22,New Orleans Saints,17,330,5674,1015,5.6
23,Chicago Bears,17,326,5233,993,5.3
24,Washington Commanders,17,321,5615,1140,4.9
25,Tampa Bay Buccaneers,17,313,5894,1159,5.1
26,Pittsburgh Steelers,17,308,5484,1109,4.9
27,Los Angeles Rams,17,307,4769,1001,4.8
28,Tennessee Titans,17,298,5045,992,5.1
29,New York Jets,17,296,5409,1074,5.0
30,Houston Texans,17,289,4820,1015,4.7
31,Indianapolis Colts,17,289,5298,1103,4.8
32,Denver Broncos,17,287,5527,1078,5.1
,Avg Team,,370.6,5760.4,1066.8,5.4
,League Total,,11860,184332,34136,5.4
,Avg Tm/G,,21.9,340.1,63.0,5.4"