Shell script to prepend text, date/time?

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Shell script to prepend text, date/time? Sat Mar 16, 2013 4:20 pm • by gcoghill
I'm looking for a way to prepend a string of text in a text file, including the current date/time. This is related to my earlier question about combining text file contents using shell scripts.

The text file to be acted upon would contain a simple chunk of text such as:
Code: Select all
Sample log file entry

I want to prepend the following to the beginning of the file:
Code: Select all
## 2013-03-16 4:00 PM:

To result in
Code: Select all
## 2013-03-16 4:00 PM: Sample log file entry


My research led me to the awk command, and I've tried some variations using the following base (which would only add the date/time, and not in the proper format, but that was my next step):

Code: Select all
awk '{print strftime("%d/%m/%y",systime()) $0 }' < "$1"

I was not able to get this to do anything at all. I tried replacing the $1 with a static file, with no luck either.

The full chain of events would be as follows:

1. .txt file is saved to a Dropbox folder
2. Hazel imports that entry to the Day One app, changes color label
3. Hazel prepends that entry with the current date & time, (## 2013-03-16 4:04 PM: ), using strftime and shell script (this is what I need help with)
4. Hazel then appends that modified content to a specific .txt file ("Master.txt")

The overall process will be to save .txt files to Dropbox (via Drafts app on iOS), import them as entries into Day One, then prepend with the hashtags and date/time, and add that modified entry to a master log file. The date/time info is redundant in Day One, hence the need for prepending. The plain text log file is just a backup document to avoid app lock-in. I am also updating that log file from other sources using IFTTT and such. Kind of like a Console.app for my life.

If easier, the initial text files are named with a date/time string in a slightly different format (2013-03-16 12-41 AM.txt) and could be used for the date/time prepending. That might even be more ideal since it will keep all the entries in sync.
gcoghill
 
Posts: 269
Joined: Tue Sep 18, 2007 8:09 am
Location: Kent, OH

Re: Shell script to prepend text, date/time? Mon Mar 18, 2013 11:58 am • by a_freyer
This tip I posted a while back might help you get started using the 'date' command.

But, this should work for you:

Code: Select all
DATE_PREPEND=$(date +"%Y-%m-%d %r")
FILE_CONTENT=$(cat "$1")
echo "##$DATE_PREPEND : $FILE_CONTENT" > "$1"
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Shell script to prepend text, date/time? Mon Mar 18, 2013 3:35 pm • by gcoghill
Once again a_freyer, thank you! This worked great. There seem to be many ways in shell scripting to achieve similar results. And that other tip looks superb as well, I have a few questions but will post on that thread.

Quick question for this one: if I wanted to instead prepend (and tweak) the file name to the contents as opposed to grabbing the current time, how would I do that?

My file names are as follows: 2013-03-17 10-32 PM.txt but I'd like that 10-32 to be 10:32.

I tried using the tips here, but when using the following script
Code: Select all
LONG_FILENAME="$1"
FILE_CONTENT=$(cat "$1")
echo "## $(basename $LONG_FILENAME) : $FILE_CONTENT" > "$1"

I get this in the text file:
Code: Select all
## _Hazel
temp
Merge
txt
file
test2.txt : Test file content.


The filename is "test2.txt" and it contained originally just the text "Test file content."
gcoghill
 
Posts: 269
Joined: Tue Sep 18, 2007 8:09 am
Location: Kent, OH

Re: Shell script to prepend text, date/time? Mon Mar 18, 2013 3:44 pm • by a_freyer
Your filename is 2013-03-17 10-32 PM.txt And you want to prepend that date to the content, while exchanging the time dash to a colon?

What would the new filename be? Same?

The reason your script didn't work is that you didn't encapsulate $LONG_FILENAME in quotes, so you ran basename over each space-delimited word in the string you passed:

Code: Select all
LONG_FILENAME="$1"
FILE_CONTENT=$(cat "$1")
echo "## $(basename "$LONG_FILENAME") : $FILE_CONTENT" > "$1"
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Shell script to prepend text, date/time? Mon Mar 18, 2013 4:12 pm • by gcoghill
Aha, thank you. I looked in that thread and it seems the OP had the code wrong. Your tweak worked perfectly.

Yes the filename would be the same, just using it to generate the date/time stamp for the content. And yes, looking to change the dash to a colon, as well as remove the file extension. I need to research the extension removal still. Thanks!
gcoghill
 
Posts: 269
Joined: Tue Sep 18, 2007 8:09 am
Location: Kent, OH

Re: Shell script to prepend text, date/time? Mon Mar 18, 2013 5:16 pm • by a_freyer
Code: Select all
filename=$(basename "$1")
extension="${filename##*.}"
filename="${filename%.*}"

FILENAME_TRANSLATED=$(echo "$filename" | awk -F "-" '{print $1"-"$2"-"$3":"$4}')
FILE_CONTENT=$(cat "$1")
echo "## $FILENAME_TRANSLATED : $FILE_CONTENT" > "$1"



Untested, but should work.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Shell script to prepend text, date/time? Mon Mar 18, 2013 5:38 pm • by gcoghill
Extreme amounts of gratitude a_freyer (it worked)! This is also helping me understand syntax, by incorporating things I want to do and seeing how they work. I really appreciate your time.
gcoghill
 
Posts: 269
Joined: Tue Sep 18, 2007 8:09 am
Location: Kent, OH


Return to Support