applescript & grep - sed command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting applescript & grep - sed command
# 1  
Old 04-25-2012
applescript & grep - sed command

I'm new using Unix commands in applescript. The following script you choose different folders with PDfs, get file count of PDfs on chosen folders, & write the results in text file.


Code:
set target_folder to choose folder with prompt "Choose target folders containing only PDFs to count files" with multiple selections allowed without invisibles
set results to ""

repeat with i from 1 to (count target_folder)
	set thisFolder to (POSIX path of item i of target_folder)
	
	(* 2 find searches need to be performed in order to have both total PDFs file count. One with all PDFs that DON'T start with letter R &  the second others that start with letter R *)


	--Find & count all PDFs in the folders selected that DON'T starts with letter R
	set fileCount to do shell script "find " & quoted form of thisFolder & " -type f  -name *.pdf -and -not -iname 'R[0-9-_]*.pdf' | wc -l"
	
	-- Delimeter results before • the bullet and convert each / to a tab
	set thisFolder to do shell script "echo " & quoted form of thisFolder & " | grep -o •.* | sed -e 's/•\\(.*\\):\\(.*\\)/\\1\\2/' -e 's/\\//        /'g"
	
	-->Results
	set results to (results & "" & thisFolder & fileCount & return)
	
	------------------------
	--Find & count all PDFs in the folders selected that starts with letter R (RESENDS)
	set fileCount to do shell script "find " & quoted form of thisFolder & " -type f -iname 'R[0-9-_]*.pdf' | wc -l"
	
	
	-- Delimeter results before • the bullet and convert each / to a tab
	set thisFolder to do shell script "echo " & quoted form of thisFolder & " | grep -o •.* | sed -e 's/•\\(.*\\):\\(.*\\)/\\1\\2/' -e 's/\\//        /'g"
	
	-->Results
	set results to (results & "" & thisFolder & "RESENDS" & fileCount & return)
	
end repeat
--For testing purposes display dialog results

----------------------------------------
--write results to "PDF File Count.txt" file
set theFilePath to (path to desktop folder as string) & "PDF File Count.txt"
set theFile to open for access file theFilePath with write permission
try
	set eof of theFile to 0
	write results to theFile
	close access theFile
on error
	close access theFile
end try

When I run this section of the script I get a total PDF file count:
-
Code:
Find & count all PDFs in the folders selected that DON'T starts with letter R
   set fileCount to do shell script "find " & quoted form of thisFolder & " -type f -name *.pdf -and -not -iname 'R[0-9-_]*.pdf' | wc -l"
   
   -- Delimeter results before • the bullet and convert each / to a tab
   set thisFolder to do shell script "echo " & quoted form of thisFolder & " | grep -o •.* | sed -e 's/•\\(.*\\):\\(.*\\)/\\1\\2/' -e 's/\\// /'g"
   
   -->Results
   set results to (results & "" & thisFolder & fileCount & return)

Running this section I'm NOT getting the total file PDF count for the files that starts with letter "R" which are RESENDS

Code:
--Find & count all PDFs in the folders selected that starts with letter R (RESENDS)
   set fileCount to do shell script "find " & quoted form of thisFolder & " -type f -iname 'R[0-9-_]*.pdf' | wc -l"
   
   
   -- Delimeter results before • the bullet and convert each / to a tab
   set thisFolder to do shell script "echo " & quoted form of thisFolder & " | grep -o •.* | sed -e 's/•\\(.*\\):\\(.*\\)/\\1\\2/' -e 's/\\// /'g"
   
   -->Results
   set results to (results & "" & thisFolder & "RESENDS" & fileCount & return)


If I delete the line that delimiters the filepath results

Code:
set thisFolder to do shell script "echo " & quoted form of thisFolder & " | grep -o •.* | sed -e 's/•\\(.*\\):\\(.*\\)/\\1\\2/' -e 's/\\// /'g"


I get the results which are a total of PDFs in the chosen folders, but then I don't need the entire file path.

/Volumes/PREP/CATALOG/2_Press_Holding/PRINT_Catalog/2012/•CUSTO_4/BODY/: 65
/Volumes/PREP/CATALOG/2_Press_Holding/PRINT_Catalog/2012/•CUSTO_4/BODY/: RESENDS 3
/Volumes/PREP/CATALOG/2_Press_Holding/PRINT_Catalog/2012/•CUSTO_4/COVERS/: 23
/Volumes/PREP/CATALOG/2_Press_Holding/PRINT_Catalog/2012/•CUSTO_4/COVERS/: RESENDS 6
/Volumes/PREP/CATALOG/2_Press_Holding/PRINT_Catalog/2012/•SMB_7/COVERS/: 15 5/Volumes/PREP/CATALOG/2_Press_Holding/PRINT_Catalog/2012/•SMB_7/COVERS/: RESENDS 5

I want to strip everything in the file path before the bullet • then for every / a tab. Something like this:

CUSTO_4 BODY 65
CUSTO_4 BODY RESENDS 3
CUSTO_4 COVERS 23
CUSTO_4 COVERS RESENDS 6
SMB_7 COVERS 15
SMB_7 COVERS RESENDS 5

What am I missing in the grep sed expresion that when I search the second time for files that starts with letter "R" I get zero

CUSTO_4 BODY 65
CUSTO_4 BODY RESENDS 0
CUSTO_4 COVERS 23
CUSTO_4 COVERS RESENDS 0
SMB_7 COVERS 15
SMB_7 COVERS RESENDS 0

I have seen this script so many times that I can't find where is the error. Sorry if this is confusing for you Im trying my best.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep & sed - Output

Hi All, Facing an issue with grep & sed I have logs as below: gsc_1_20121121.log:2012-11-21 10:09:13,143 INFO - fmsspace.1 ProcessNewOrderSingleRequest: Result - ProcessNewOrderSingleBatchResultDTO - success:true,newOrderSingleBatchResults:ProcessNewOrderSingleResultDTO -... (13 Replies)
Discussion started by: irfanmemon
13 Replies

2. Shell Programming and Scripting

help understanding regex with grep & sed

I have the following line of code that works wonders. I just don't completely understand it as I am just starting to learn regex. Can you help me understand exactly what is happening here? find . -type f | grep -v '^\.$' | sed 's!\.\/!!' (4 Replies)
Discussion started by: trogdortheburni
4 Replies

3. Homework & Coursework Questions

sed & cut command issues

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: After using the egrep command to pull certain lines from the asg5f1 (creating the asg5f1c file), I am required... (1 Reply)
Discussion started by: robrom78
1 Replies

4. Shell Programming and Scripting

SED command works in terminal, but not Applescript

The following command works perfectly in Terminal, but not in Applescript. (Returns "unknown token" error for square brackets.) (new to site. sorry.) I have an Applescript that is designed to find and remove any square-bracketed text, including the square brackets. I ran the following code from... (1 Reply)
Discussion started by: Phillip Acosta
1 Replies

5. Shell Programming and Scripting

Help Needed with grep & sed

On one of my servers, it appears that a bunch of html files got the following code added to it... I was going to try to remove this line using grep & sed... as sample grep -lr -e 'apples' *.html | xargs sed -i 's/apples/oranges/g' I can get the grep portion to work... grep "<script... (7 Replies)
Discussion started by: djlane
7 Replies

6. UNIX for Dummies Questions & Answers

Grep char count & pipe to sed command

Hi I am having a 'grep' headache Here is the contents of my file: (PBZ,CP,(((ME,PBZ,BtM),ON),((ME,((PBZ,DG),(CW9,PG11))),CW9,TS2,RT1))) I would like to count out how many times 'PBZ' occurs and then place that number in the line above 3... (8 Replies)
Discussion started by: cavanac2
8 Replies

7. OS X (Apple)

Need help writing an Applescript to launch a specific Terminal Command...

I developed a script in Lingon (which is an automated script editor developed for OS X) that is used to automatically restart programs only if they crash. The script itself does just that, but I only want it to load if I'm going to use the specific application that it's designed to protect. In the... (2 Replies)
Discussion started by: JFraser1
2 Replies

8. Shell Programming and Scripting

Need help writing an Applescript to launch a specific Terminal Command...

I developed a script in Lingon (which is an automated script editor developed for OS X) that is used to automatically restart programs only if they crash. The script itself does just that, but I only want it to load if I'm going to use the specific application that it's designed to protect. In... (3 Replies)
Discussion started by: JFraser1
3 Replies

9. Shell Programming and Scripting

grep & sed question

I'm trying to write a bash script to perform a tedious task, but I have no experience and hardly any knowledge so I've been having a rough time with it. I'm on Mac OS X, and I want a script to do the following: I have a directory that has about 200 sudirectories. In each of these directories,... (1 Reply)
Discussion started by: der Kopf
1 Replies

10. UNIX for Dummies Questions & Answers

grep command & shell procedure's

hello, i'm trying to pluck words from a list that have exactly 3 occurances of a specified letter. I've come up with this : grep -i .*$1.*$1.*$1.* But this also selects words with 4 or more, any tips? I'm putting this into a shell procedure and want to be able to add a switch before I put the... (1 Reply)
Discussion started by: stevox
1 Replies
Login or Register to Ask a Question