Jump to content

Making changes to a text file using AppleScript


ramin85
 Share

4 posts in this topic

Recommended Posts

Hi,

 

I would apologize if my first post on these forum is a question, but I've looked around without any luck and only found these forums by looking for an answer to my question!

 

So, I would like to automate the following tasks to all the files inside a "log" folder. The logs generated by this specific program contains too many irrelevant information which I would like to clean up automatically, before going through the each log, every day!

 

1- Look for files inside a folder.

2- Delete n number of lines in the beginning of the text file

3- Delete every other line in the text file.

4- Delete n number of lines in the end of the file.

5- Save to a new destination

 

I would greatly appreciate if someone could direct me into the right direction! :)

Link to comment
Share on other sites

thanks for the quick reply,

 

I was able to find information about sed; however nothing about cron, head, or tail. Would you please explain a bit more, I'm really new to AppleScript. I used to compile applications using Fortran77 back in the day, but I'm too old now for the adventures!

 

Appreciate your help

Link to comment
Share on other sites

thanks for the quick reply,

 

I was able to find information about sed; however nothing about cron, head, or tail. Would you please explain a bit more, I'm really new to AppleScript. I used to compile applications using Fortran77 back in the day, but I'm too old now for the adventures!

 

Appreciate your help

 

As you should have found sed will manipulate text files so you can use it for the deletions needed, head will give you the first number of lines from the top that you specify this can be used for getting rid of that you need, tail is the bottom of the file listing that can be used for that part and cron is a task scheduler you would use it to run a script that contains the necessary commands for processing your log files at whatever time of day you want them processed. These commands are not applescript but basic unix/shell commands that have been around for decades.

 

Basically what you need to do is first of all copy your files to be processed to backup directory so you are not going to mess up the original files when testing out the script you need to create to do the processing. Now a basic bash script starter for processing all the log files in a directory you run the script from.

 

 

#!/bin/bash

# Change to directory to be processed
cd /path/to/files/to/be/processed

# Now process all .log files under that directory
for i in $(find $PWD | grep .log ); do
    # Name of file to use
    NAME=$(basename "$i")
    # processing of file
    head $NAME
    tail $NAME
    sed $NAME
 
done
 

 

Now you would need to figure out the exact commands you need for the processing part to accomplish what you need done. The cron part is totally easy using crontab -e then something like the below if you want it.

 

 

@daily /path/to/script.sh

 

Otherwise replace the @daily with the time of day for it to run of course having done chmod +x /path/to/script.sh to make the script executable.

Link to comment
Share on other sites

 Share

×
×
  • Create New...