Hey Ali,
This is professional work you’re asking for…
I’ll give you an example using the Satimage.osax AppleScript Extension, because it didn’t take me too long to design and write.
Install the latest version from (here) – particularly if you’re using macOS Sierra.
The script will ask you to pick a .csv file and will process it into a folder named Split_CSV_File_Output in your ~/Downloads folder.
This script will ONLY work with csv files where the sort field is the FIRST field of the record – like you’ve shown in the image you posted.
------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/03/18 21:33
# dMod: 2017/03/19 15:20
# Appl: Satimage.osax
# Task: Process a CSV file and output records with the same value in a specific field.
# Libs: None
# Osax: Satimage.osax is REQUIRED!
# Tags: @Applescript, @Script, @Finder, @Process, @CSV, @File, @Output, @Records, @Specific, @Field, @ccstone
------------------------------------------------------------------------------
try
set outputFolderName to "Split_CSV_File_Output"
set outputFolderPathHFS to (path to downloads folder as text) & outputFolderName & ":"
set collatorList to {}
tell application "Finder"
activate
if (folder outputFolderPathHFS exists) = false then
make new folder at (path to downloads folder) with properties {name:outputFolderName}
end if
open outputFolderPathHFS as alias
set inputFile to choose file with prompt "Pick a CSV file to process:" of type "CSV" default location (path to desktop)
end tell
set csvText to readtext inputFile
set csvText to cng("\\A\\s+|\\s+\\Z", "", csvText) of me
set AppleScript's text item delimiters to ","
set errorCheckCounter to 0
set errorCheckLimit to 10000
repeat while fndBool("^\\S+", csvText, false, false) of me = true
if errorCheckCounter = errorCheckLimit then error "Error - Infinite Loop?"
set field1 to first text item of csvText
set tempList to fnd("^" & field1 & ".+", csvText, true, true) of me
if tempList ≠ {} then
set outputFilePath to outputFolderPathHFS & field1 & ".csv"
writetext (join tempList using linefeed) to file outputFilePath
set csvText to cng("^" & field1 & ".+\\R?", "", csvText) of me
end if
set errorCheckCounter to errorCheckCounter + 1
end repeat
on error e number n
set e to e & return & return & "Num: " & n
if n ≠ -128 then
try
tell application (path to frontmost application as text) to set ddButton to button returned of ¬
(display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
default button "OK" giving up after 30)
if ddButton = "Copy Error Message" then set the clipboard to e
end try
end if
end try
------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on cng(_find, _replace, _data)
change _find into _replace in _data with regexp without case sensitive
end cng
------------------------------------------------------------------------------
on fnd(_find, _data, _all, strRslt)
try
find text _find in _data all occurrences _all string result strRslt with regexp without case sensitive
on error
return false
end try
end fnd
------------------------------------------------------------------------------
on fndBool(_find, _data, _all, strRslt)
try
find text _find in _data all occurrences _all string result strRslt with regexp without case sensitive
return true
on error
return false
end try
end fndBool
------------------------------------------------------------------------------
Considerable customization is possible of course.
-Chris