Jump to content
ramin85

Making changes to a text file using AppleScript

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! :)

Share this post


Link to post
Share on other sites
Advertisement

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×