5 Replies Latest reply: May 12, 2012 9:51 AM by Frinton Fogey
Frinton Fogey Level 1 Level 1 (0 points)

I want to analyse record types from a large input file and so far have managed to extract different groups and created the relevant files. All I want to do now is count the number of records in each file so I can produce a report showing the different types but cannot figure out how to do this, I know it will be simple but I cannot get any command to accomplish this, help please!


iMac, Mac OS X (10.7.1)
  • 1. Re: Determine the number of records in a file
    twtwtw Level 5 Level 5 (4,690 points)

    'Records in a file' is not specific enough for us to answer.  what kind of file are you talking about? what kind of records?  what's the tpical structure of a record?

  • 2. Re: Determine the number of records in a file
    Frinton Fogey Level 1 Level 1 (0 points)

    Sorry for lack of detail, right, the records come from emails which were converted via TextEdit to one large .txt file. The emails consisted of requests for different services so I then ran various grep commands to strip out and create .txt files by each type of request, i.e. 'Want to make a donation' or 'Want to adopt a cat'. I now have numerous .txt files which contain records of all the same type and all I need is to be able to count the number of records in each file so we can analyse the phone calls which come in as emails from a call centre. I can do it manually but there must be a simple solution. Hope this helps.

  • 3. Re: Determine the number of records in a file
    Jeffrey Jones2 Level 6 Level 6 (8,510 points)

    What do you mean by "records"? Do you mean "lines of text"? In that case, wc:

     

    wc -l file.txt

  • 4. Re: Determine the number of records in a file
    twtwtw Level 5 Level 5 (4,690 points)

    in applescript, there are a few ways to count patterned text.  if (as jeffrey suggests) each record is a separate line, then you can use the following:

     

    set textContents to read "/path/to/file.txt"

    count of paragraphs of textContents

     

    if records are determined by some other process (such as a special delimiting character or phrase) you can use text items (replace the @ sign with the special character or phrase):

     

    set textContents to read "/path/to/file.txt"

    set {oldTID, my text item delimiters} to {my text item delimiters, "@"}

    set theCount to count of text items of textContents

    set my text item delimiters to oldTID

  • 5. Re: Determine the number of records in a file
    Frinton Fogey Level 1 Level 1 (0 points)

    Brilliant, the first solution works perfectly, never would have thought of using 'paragraphs', many thanks.