2

I am running a script file. I have written a while loop inside the script in which I have pipelined multiple commands like awk, sed and finally I am writing the output to a file using > operator. I am trying to add a time stamp at the starting line of the file (I am not appending the file).

Each time I run the script it should write the time stamp something like below to the file:

Time stamp

Data of the File

techraf
  • 5,941
RajuBhai
  • 191

5 Answers5

2

I say create a second script like this:

#!/bin/bash
echo -e "$(date)\n" # of course format date to your needs
cat <&0
exit $?

This will work like: (... all your commands & pipes...) | new_script >output.file

Explained:

First echo the date and a newline, then catenate STDIN and finally exit with the last (cat's) exit code.

Dalvenjia
  • 2,026
0

It would probably be easiest to simply write the time via date to the file and then append your data to it afterwards, something like this:

date > file
your_code >> file

Look into the manpage of date to find proper the proper format for your usecase

0

add this following or preceding your line `date "+%Y-%m-%d"` e.g. 2016-04-26

in your code printf "this is the output\t`date "+%Y-%m-%d"`"

will read as:

this is the output   2016-04-26

you can use this with echo too

you can do others like:

`date "+%b%d"` e.g. Apr-26

for other formats check man date

additional info:

` (Back Quotes) : Command Substitution

The ` character (found on the key with the ~) is very important when used in shell commands. This ` indicates that command substitution is required wherever it is used. Hence whenever ` is used, whatever part of the command is enclosed by these Backquotes marks would be executed (as if it was the only command) and then the result of that command would be substituted in the original shell command that you typed. The following explains this clearly

Source: http://www.codecoffee.com/tipsforlinux/articles/26-2.html

Kramer
  • 166
0

Use gawk, and its time functions like in this example:

ping 127.0.0.1 | gawk '{print strftime("%H-%M-%S") ; print $0}'

Oops, I just saw, that the OP wants one timestamp at the beginning of the output, not one per input line like my suggestion. His mention of the while loop though implies to me that data will keep arriving for a while, and he may want to measure time2output for each of them at one point.

  • Thanks for replying. Actually my next question was this ! But I am trying to print the time in milliseconds !! Is there any way to do that ? – RajuBhai Apr 26 '16 at 23:19
0

"logger"

You might also want to take a look at logger, a somewhat neglected command for writing messages to the system log (or journal) in the system's standard log output format. This won't help you if your application maintains its own logfile, but depending on the circumstances, there might be good design rationales to use the standard system logging facilities instead. Just bringing this up in case, since logger adds the timestamps for you.

Guido
  • 4,114
  • This should be the first place to start, IMO. Reinventing loggers is not a good use of time for most of us. –  Apr 26 '16 at 18:04