Page 1 of 1

Shell script to combine files?

PostPosted: Sat Mar 09, 2013 2:34 am
by gcoghill
Another script question: I am trying to set up a Hazel rule which takes new text files, and adds them to a master text file (Master.txt), adding two return characters in between.

This
Code: Select all
cat $1 >> Master.txt

isn't working.

I'm not sure how to get it to add the returns, and any help on coding for file names with spaces is appreciated as well.

Final result should contain this in Master.txt:

Code: Select all
2013-03-09 1:13 AM:
entry from file1.txt

2013-03-09 1:13 AM:
entry from file2.txt


Each text file contains this:

Code: Select all
2013-03-09 1:13 AM:
entry from file2.txt

Re: Shell script to combine files?

PostPosted: Mon Mar 11, 2013 11:56 am
by Mr_Noodle
You can try using 'printf' to add the newlines:
Code: Select all
printf "\n\n" >> Master.txt

Re: Shell script to combine files?

PostPosted: Wed Mar 13, 2013 9:54 am
by gcoghill
Thanks Mr_Noodle. I'm not currently able to get the file content to merge, I tout maybe I had modified the code for Hazel improperly. The standard shell script to merge file contents seemed pretty straightforward as did the code to substitute for use with Hazel but no luck.

Re: Shell script to combine files?

PostPosted: Wed Mar 13, 2013 10:57 am
by a_freyer
So you're trying to append the content of each of these text files to the the end of Master.txt with an additional new line between each?

If so, what Mr_Noodle said does work. To be absolutely clear, this is what we'll have:

Code: Select all
if (all) of the following conditions are met for (the file or folder being processed):
     extension is txt

Do the following to the matched file or folder:
     run shell script (embedded script)



Where the embedded script is:

Code: Select all
cat "$1" >> Master.txt
printf "\n\n" >> Master.txt


NOTE: Perhaps your issue is that you are not enclosing $1 in quotations, as I have shown above.

Re: Shell script to combine files?

PostPosted: Wed Mar 13, 2013 11:37 am
by gcoghill
Aha, the quotes around the $1 was probably the issue. I'll give that a try, thank you!

Re: Shell script to combine files?

PostPosted: Wed Mar 13, 2013 12:37 pm
by gcoghill
That's not working for some reason. The rule runs and turns the file color label green, but the script doesn't do what its supposed to do.

Image
If I do not have an existing Master.txt file, it does create one but the file is not a text file. My intention is to have a log file (Master.txt) that gets appended with ongoing content from individual text files.

Re: Shell script to combine files?

PostPosted: Wed Mar 13, 2013 12:41 pm
by a_freyer
There is nothing wrong with your script, or your rule. Have you checked the file content?

Also, when you say "The script isn't working" what do you mean? Does that mean that the script does not add anything to Master.txt? Does that mean that the newlines are not being added?

Re: Shell script to combine files?

PostPosted: Wed Mar 13, 2013 12:57 pm
by a_freyer

Re: Shell script to combine files?

PostPosted: Wed Mar 13, 2013 1:49 pm
by gcoghill
So I discovered that the new lines are being added, however the text from the other files is not being appended. Originally I assumed neither was working.

Re: Shell script to combine files?

PostPosted: Wed Mar 13, 2013 1:54 pm
by a_freyer
There is something else going on here. As shown above, the rule functions fine on my machine.

I suggest deleting master. txt and clearing the color label of all of your text files to start over.

Re: Shell script to combine files?

PostPosted: Wed Mar 13, 2013 2:12 pm
by gcoghill
Yes something odd is happening. I deleted all the files and replaced with new ones, even set the Master.txt to the full file path but no luck. I checked file permissions on the Master.txt as well.

Thank you for the help, looks like I've got something else going on here.

Re: Shell script to combine files?

PostPosted: Sat Mar 16, 2013 1:06 am
by gcoghill
For some reason this wasn't working on my MacBook, but just tested on the Mac Pro and all is working perfectly. Thanks again for the help!

Re: Shell script to combine files?

PostPosted: Mon Mar 18, 2013 12:06 pm
by a_freyer
Potentially a permissions issue on the MacBook. Make sure that you have write permissions as the logged in user for the directory of Master.txt

Re: Shell script to combine files?

PostPosted: Mon Mar 18, 2013 2:50 pm
by gcoghill
I checked the permissions, but it's possible I overlooked something. I was out of town last week and my attention was a bit divided. Thanks again for the help!