Excel sheet sort and extract from KM

I have a master .csv file separated by comma, I would like to pull out all rows with the identical fields in a column named 'category' into a separate file and then preferably encrypt this too with a particular password (this last part is optional)....

e.g. let's say that I have 10 rows in the file with many columns. One of the column is named 'category' the rows that have identical fields for 'category' should be pulled out into a separate file.

category of fruits, and the rows have the name of the fruit in the column under the heading 'category' there are 3 rows which have apple in them and 7 which state 'mandarin'. I would like a new .csv to be created where all 3 rows of 'apple' are there and another .csv file with the 'mandarin'

Is this possible if so how? basically the function is to search and pull out all rows where the cell under the column named 'category' is identical.

Many thanks for your assistance.

Here is an image and I would like to pull out all rows and put them into separate files where the cells under the column named Defendant name match. In this case all rows with RBS in the column named 'Defendant name' would all be in the file and a new file would be created each for 'Oracel Financial...', 'RBS' and 'Royal Bank of Scotl...' these are not all the possible names.

Since your workflow is 100% within Excel, you might consider using Excel VBA or AppleScript for your solution. I think a KM solution would need to use AppleScript.

But don't overlook Excel VBA. It is very powerful, and easy to use/learn.
You can start by recording an Excel macro of the steps you need to use.

1 Like

Hiya, no its not in excel its just a .csv file.

Well, since your topic title is "Excel sheet sort and extract from KM", it let me to believe you were using Excel.

Another option is to use JXA (JavaScript) to read the CSV file, and create a separate CSV file for each unique value in the designated column ("Defendant name" or "category", etc).

This is definitely doable, but would take some work.

1 Like

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

1 Like

Hi @ccstone many thanks for this,

  1. Is there a way to get the headings of the column from the master file copied into each file?
  2. Is it possible to have the name of the banks appear after the name of the actual file? e.g. if the name of the file is ‘Overdue Acknowledgement cases’ the files created should have this name as a prefix ie Overdue Acknowledgement cases - Barclays

Many thanks for your assistance.

Hey Ali,

Yes to both 1 and 2.

I’ll look into it.

-Chris