<p>I ended up writing a quick little script for this in Python, but I was wondering if there was a utility you could feed text into which would prepend each line with some text -- in my specific case, a timestamp.  Ideally, the use would be something like:</p>

<pre><code>cat somefile.txt | prepend-timestamp
</code></pre>

<p>(Before you answer sed, I tried this:</p>

<pre><code>cat somefile.txt | sed "s/^/`date`/"
</code></pre>

<p>But that only evaluates the date command once when sed is executed, so the same timestamp is incorrectly prepended to each line.)</p>

[{'answer_id': 21602, 'author': 'jj33', 'author_id': 430, 'author_profile': 'https://Stackoverflow.com/users/430', 'pm_score': 5, 'selected': False, 'text': '<p>How about this?</p>\n\n<pre><code>cat somefile.txt | perl -pne \'print scalar(localtime()), " ";\'\n</code></pre>\n\n<p>Judging from your desire to get live timestamps, maybe you want to do live updating on a log file or something?  Maybe</p>\n\n<pre><code>tail -f /path/to/log | perl -pne \'print scalar(localtime()), " ";\' &gt; /path/to/log-with-timestamps\n</code></pre>\n'}, {'answer_id': 21611, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': -1, 'selected': False, 'text': "<p>If the value you are prepending is the same on every line, fire up emacs with the file, then:</p>\n\n<p><kbd>Ctrl</kbd> + <kbd>&lt;space&gt;</kbd></p>\n\n<p>at the beginning of the of the file (to mark that spot), then scroll down to the beginning of the last line (Alt + &gt; will go to the end of file... which probably will involve the Shift key too, then Ctrl + a to go to the beginning of that line) and:</p>\n\n<p><kbd>Ctrl</kbd> + <kbd>x</kbd> <kbd>r</kbd> <kbd>t</kbd></p>\n\n<p>Which is the command to insert at the rectangle you just specified (a rectangle of 0 width).</p>\n\n<p>2008-8-21 6:45PM &lt;enter&gt;</p>\n\n<p>Or whatever you want to prepend... then you will see that text prepended to every line within the 0 width rectangle.</p>\n\n<p>UPDATE: I just realized you don't want the SAME date, so this won't work... though you may be able to do this in emacs with a slightly more complicated custom macro, but still, this kind of rectangle editing is pretty nice to know about...</p>\n"}, {'answer_id': 21614, 'author': 'PabloG', 'author_id': 394, 'author_profile': 'https://Stackoverflow.com/users/394', 'pm_score': 2, 'selected': False, 'text': '<p>I\'m not an Unix guy, but I think you can use</p>\n\n<pre><code>gawk \'{print strftime("%d/%m/%y",systime()) $0 }\' &lt; somefile.txt\n</code></pre>\n'}, {'answer_id': 21620, 'author': 'Kieron', 'author_id': 588, 'author_profile': 'https://Stackoverflow.com/users/588', 'pm_score': 9, 'selected': True, 'text': '<p>Could try using <code>awk</code>:</p>\n\n<pre><code>&lt;command&gt; | awk \'{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush(); }\'\n</code></pre>\n\n<p>You may need to make sure that <code>&lt;command&gt;</code> produces line buffered output, i.e. it flushes its output stream after each line; the timestamp <code>awk</code> adds will be the time that the end of the line appeared on its input pipe.</p>\n\n<p>If awk shows errors, then try <code>gawk</code> instead.</p>\n'}, {'answer_id': 21907, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 4, 'selected': False, 'text': '<p>Kieron\'s answer is the best one so far.  If you have problems because the first program is buffering its out you can use the unbuffer program:</p>\n\n<pre><code>unbuffer &lt;command&gt; | awk \'{ print strftime("%Y-%m-%d %H:%M:%S"), $0; }\'\n</code></pre>\n\n<p>It\'s installed by default on most linux systems.  If you need to build it yourself it is part of the expect package</p>\n\n<p><a href="http://expect.nist.gov" rel="noreferrer">http://expect.nist.gov</a></p>\n'}, {'answer_id': 21909, 'author': 'T Percival', 'author_id': 954, 'author_profile': 'https://Stackoverflow.com/users/954', 'pm_score': 6, 'selected': False, 'text': '<p><a href="http://jeroen.a-eskwadraat.nl/sw/annotate/annotate" rel="noreferrer">annotate</a>, available via that link or as <code>annotate-output</code> in the Debian <code>devscripts</code> package.</p>\n\n<pre><code>$ echo -e "a\\nb\\nc" &gt; lines\n$ annotate-output cat lines\n17:00:47 I: Started cat lines\n17:00:47 O: a\n17:00:47 O: b\n17:00:47 O: c\n17:00:47 I: Finished with exitcode 0\n</code></pre>\n'}, {'answer_id': 22441, 'author': 'caerwyn', 'author_id': 2406, 'author_profile': 'https://Stackoverflow.com/users/2406', 'pm_score': 3, 'selected': False, 'text': '<p>Use the read(1) command to read one line at a time from standard input, then output the line prepended with the date in the format of your choosing using date(1).</p>\n\n<pre><code>$ cat timestamp\n#!/bin/sh\nwhile read line\ndo\n  echo `date` $line\ndone\n$ cat somefile.txt | ./timestamp\n</code></pre>\n'}, {'answer_id': 436536, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<pre><code>#! /bin/sh\nunbuffer "$@" | perl -e \'\nuse Time::HiRes (gettimeofday);\nwhile(&lt;&gt;) {\n        ($s,$ms) = gettimeofday();\n        print $s . "." . $ms . " " . $_;\n}\'\n</code></pre>\n'}, {'answer_id': 5158624, 'author': 'ElGringoGeek', 'author_id': 639872, 'author_profile': 'https://Stackoverflow.com/users/639872', 'pm_score': 2, 'selected': False, 'text': '<p>Here\'s my awk solution (from a Windows/XP system with MKS Tools installed in the C:\\bin directory). It is designed to add the current date and time in the form mm/dd hh:mm to the beginning of each line having fetched that timestamp from the system as each line is read.  You could, of course, use the BEGIN pattern to fetch the timestamp once and add that timestamp to each record (all the same).  I did this to tag a log file that was being generated to stdout with the timestamp at the time the log message was generated.</p>\n\n<p><code>/"pattern"/      "C\\:\\\\\\\\bin\\\\\\\\date \'+%m/%d %R\'" | getline timestamp;</code><Br/>\n<code>print timestamp, $0;</code></p>\n\n<p>where "pattern" is a string or regex (without the quotes) to be matched in the input line, and is optional if you wish to match all input lines.</p>\n\n<p>This should work on Linux/UNIX systems as well, just get rid of the C\\:\\\\bin\\\\ leaving the line</p>\n\n<pre><code>             "date \'+%m/%d %R\'" | getline timestamp;\n</code></pre>\n\n<p>This, of course, assumes that the command "date" gets you to the standard Linux/UNIX date display/set command without specific path information (that is, your environment PATH variable is correctly configured).</p>\n'}, {'answer_id': 9613514, 'author': 'chazomaticus', 'author_id': 30497, 'author_profile': 'https://Stackoverflow.com/users/30497', 'pm_score': 4, 'selected': False, 'text': '<p>Just gonna throw this out there: there are a pair of utilities in <a href="http://cr.yp.to/daemontools.html">daemontools</a> called <a href="http://cr.yp.to/daemontools/tai64n.html">tai64n</a> and <a href="http://cr.yp.to/daemontools/tai64nlocal.html">tai64nlocal</a> that are made for prepending timestamps to log messages.</p>\n\n<p>Example:</p>\n\n<pre><code>cat file | tai64n | tai64nlocal\n</code></pre>\n'}, {'answer_id': 9813614, 'author': 'Mark McKinstry', 'author_id': 712655, 'author_profile': 'https://Stackoverflow.com/users/712655', 'pm_score': 8, 'selected': False, 'text': '<p><code>ts</code> from <a href="http://kitenet.net/~joey/code/moreutils/" rel="noreferrer">moreutils</a> will prepend a timestamp to every line of input you give it. You can format it using strftime too.</p>\n\n<pre><code>$ echo \'foo bar baz\' | ts\nMar 21 18:07:28 foo bar baz\n$ echo \'blah blah blah\' | ts \'%F %T\'\n2012-03-21 18:07:30 blah blah blah\n$ \n</code></pre>\n\n<p>To install it:</p>\n\n<pre><code>sudo apt-get install moreutils\n</code></pre>\n'}, {'answer_id': 18923989, 'author': 'crumplecrap', 'author_id': 2800436, 'author_profile': 'https://Stackoverflow.com/users/2800436', 'pm_score': 1, 'selected': False, 'text': "<p>caerwyn's answer can be run as a subroutine, which would prevent the new processes per line:</p>\n\n<pre><code>timestamp(){\n   while read line\n      do\n         echo `date` $line\n      done\n}\n\necho testing 123 |timestamp\n</code></pre>\n"}, {'answer_id': 19662206, 'author': 'Willem', 'author_id': 604515, 'author_profile': 'https://Stackoverflow.com/users/604515', 'pm_score': 5, 'selected': False, 'text': '<p>Distilling the given answers to the simplest one possible:</p>\n\n<pre><code>unbuffer $COMMAND | ts\n</code></pre>\n\n<p>On Ubuntu, they come from the expect-dev and moreutils packages.</p>\n\n<pre><code>sudo apt-get install expect-dev moreutils\n</code></pre>\n'}, {'answer_id': 27001032, 'author': 'orion elenzil', 'author_id': 230851, 'author_profile': 'https://Stackoverflow.com/users/230851', 'pm_score': 0, 'selected': False, 'text': '<p>doing it with <code>date</code> and <code>tr</code> and <code>xargs</code> on OSX:</p>\n\n<pre><code>alias predate="xargs -I{} sh -c \'date +\\"%Y-%m-%d %H:%M:%S\\" | tr \\"\\n\\" \\" \\"; echo \\"{}\\"\'"\n&lt;command&gt; | predate\n</code></pre>\n\n<p>if you want milliseconds:</p>\n\n<pre><code>alias predate="xargs -I{} sh -c \'date +\\"%Y-%m-%d %H:%M:%S.%3N\\" | tr \\"\\n\\" \\" \\"; echo \\"{}\\"\'"\n</code></pre>\n\n<p>but note that on OSX, date doesn\'t give you the %N option, so you\'ll need to install gdate (<code>brew install coreutils</code>) and so finally arrive at this:</p>\n\n<pre><code>alias predate="xargs -I{} sh -c \'gdate +\\"%Y-%m-%d %H:%M:%S.%3N\\" | tr \\"\\n\\" \\" \\"; echo \\"{}\\"\'"\n</code></pre>\n'}, {'answer_id': 33218664, 'author': 'Keymon', 'author_id': 395686, 'author_profile': 'https://Stackoverflow.com/users/395686', 'pm_score': 2, 'selected': False, 'text': '<p>Mixing some answers above from <a href="https://stackoverflow.com/users/179583/natevw">natevw</a> and Frank Ch. Eigler.</p>\n\n<p>It has milliseconds, performs better than calling a external <code>date</code> command each time and perl can be found in most of the servers.</p>\n\n<pre><code>tail -f log | perl -pne \'\n  use Time::HiRes (gettimeofday);\n  use POSIX qw(strftime);\n  ($s,$ms) = gettimeofday();\n  print strftime "%Y-%m-%dT%H:%M:%S+$ms ", gmtime($s);\n  \'\n</code></pre>\n\n<p>Alternative version with flush and read in a loop:</p>\n\n<pre><code>tail -f log | perl -pne \'\n  use Time::HiRes (gettimeofday); use POSIX qw(strftime);\n  $|=1;\n  while(&lt;&gt;) {\n    ($s,$ms) = gettimeofday();\n    print strftime "%Y-%m-%dT%H:%M:%S+$ms $_", gmtime($s);\n  }\'\n</code></pre>\n'}, {'answer_id': 44746681, 'author': 'Momchil Atanasov', 'author_id': 2852445, 'author_profile': 'https://Stackoverflow.com/users/2852445', 'pm_score': 1, 'selected': False, 'text': '<p><strong>Disclaimer</strong>: the solution I am proposing is not a Unix built-in utility.</p>\n\n<p>I faced a similar problem a few days ago. I did not like the syntax and limitations of the solutions above, so I quickly put together a program in Go to do the job for me.</p>\n\n<p>You can check the tool here: <strong><a href="https://github.com/mokiat/preftime" rel="nofollow noreferrer">preftime</a></strong></p>\n\n<p>There are prebuilt executables for Linux, MacOS, and Windows in the <a href="https://github.com/mokiat/preftime/releases" rel="nofollow noreferrer">Releases</a> section of the GitHub project.</p>\n\n<p>The tool handles incomplete output lines and has (from my point of view) a more compact syntax.</p>\n\n<p><code>&lt;command&gt; | preftime</code></p>\n\n<p>It\'s not ideal, but I though I\'d share it in case it helps someone.</p>\n'}, {'answer_id': 44786469, 'author': 'aleksandr barakin', 'author_id': 4827341, 'author_profile': 'https://Stackoverflow.com/users/4827341', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n<pre><code>$ cat somefile.txt | sed "s/^/`date`/"\n</code></pre>\n</blockquote>\n\n<p>you can do this (with <em>gnu/sed</em>):</p>\n\n<pre><code>$ some-command | sed "x;s/.*/date +%T/e;G;s/\\n/ /g"\n</code></pre>\n\n<p>example:</p>\n\n<pre><code>$ { echo \'line1\'; sleep 2; echo \'line2\'; } | sed "x;s/.*/date +%T/e;G;s/\\n/ /g"\n20:24:22 line1\n20:24:24 line2\n</code></pre>\n\n<p>of course, you can use other options of the program <em>date</em>. just replace <code>date +%T</code> with what you need.</p>\n'}, {'answer_id': 71806202, 'author': 'Lucas Wiman', 'author_id': 303931, 'author_profile': 'https://Stackoverflow.com/users/303931', 'pm_score': 1, 'selected': False, 'text': '<p>The other answers mostly work, but have some drawbacks. In particular:</p>\n<ol>\n<li>Many require installing a command not commonly found on linux systems, which may not be possible or convenient.</li>\n<li>Since they use pipes, they don\'t put timestamps on stderr, and lose the exit status.</li>\n<li>If you use multiple pipes for stderr and stdout, then some do not have atomic printing, leading to intermingled lines of output like <code>[timestamp] [timestamp] stdout line \\nstderr line</code></li>\n<li>Buffering can cause problems, and <code>unbuffer</code> requires an extra dependency.</li>\n</ol>\n<p>To solve (4), we can use <code>stdbuf -i0 -o0 -e0</code> which is generally available on most linux systems (see <a href="https://stackoverflow.com/questions/3465619/how-to-make-output-of-any-shell-command-unbuffered">How to make output of any shell command unbuffered?</a>).</p>\n<p>To solve (3), you just need to be careful to print the entire line at a time.</p>\n<ul>\n<li>Bad: <code>ruby -pe \'print Time.now.strftime(\\&quot;[%Y-%m-%d %H:%M:%S] \\&quot;)\'</code> (Prints the timestamp, then prints the contents of <code>$_</code>.)</li>\n<li>Good: <code>ruby -pe \'\\$_ = Time.now.strftime(\\&quot;[%Y-%m-%d %H:%M:%S] \\&quot;) + \\$_\'</code> (Alters <code>$_</code>, then prints it.)</li>\n</ul>\n<p>To solve (2), we need to use multiple pipes and save the exit status:</p>\n<pre class="lang-sh prettyprint-override"><code>alias tslines-pipe=&quot;stdbuf -i0 -o0 ruby -pe \'\\$_ = Time.now.strftime(\\&quot;[%Y-%m-%d %H:%M:%S] \\&quot;) + \\$_\'&quot;\nfunction tslines() (\n  stdbuf -o0 -e0 &quot;$@&quot; 2&gt; &gt;(tslines-pipe) &gt; &gt;(tslines-pipe)\n  status=&quot;$?&quot;\n  exit $status\n)\n</code></pre>\n<p>Then you can run a command with <code>tslines some command --options</code>.</p>\n<p>This <em>almost</em> works, except sometimes one of the pipes takes slightly longer to exit and the <code>tslines</code> function has exited, so the next prompt has printed. For example, this command seems to print all the output after the prompt for the next line has appeared, which can be a bit confusing:</p>\n<pre><code>tslines bash -c \'(for (( i=1; i&lt;=20; i++ )); do echo stderr 1&gt;&amp;2; echo stdout;  done)\'\n</code></pre>\n<p>There needs to be some coordination method between the two pipe processes and the tslines function. There are presumably many ways to do this. One way I found is to have the pipes send some lines to a pipe that the main function can listen to, and only exit <em>after</em> it\'s received data from both pipe handlers. Putting that together:</p>\n<pre class="lang-sh prettyprint-override"><code>alias tslines-pipe=&quot;stdbuf -i0 -o0 ruby -pe \'\\$_ = Time.now.strftime(\\&quot;[%Y-%m-%d %H:%M:%S] \\&quot;) + \\$_\'&quot;\nfunction tslines() (\n  # Pick a random name for the pipe to prevent collisions.\n  pipe=&quot;/tmp/pipe-$RANDOM&quot;\n  \n  # Ensure the pipe gets deleted when the method exits.\n  trap &quot;rm -f $pipe&quot; EXIT\n\n  # Create the pipe.  See https://www.linuxjournal.com/content/using-named-pipes-fifos-bash\n  mkfifo &quot;$pipe&quot;\n  # echo will block until the pipe is read.\n  stdbuf -o0 -e0 &quot;$@&quot; 2&gt; &gt;(tslines-pipe; echo &quot;done&quot; &gt;&gt; $pipe) &gt; &gt;(tslines-pipe; echo &quot;done&quot; &gt;&gt; $pipe)\n  status=&quot;$?&quot;\n\n  # Wait until we\'ve received data from both pipe commands before exiting.\n  linecount=0\n  while [[ $linecount -lt 2 ]]; do\n    read line\n    if [[ &quot;$line&quot; == &quot;done&quot; ]]; then\n      ((linecount++))\n    fi\n  done &lt; &quot;$pipe&quot;\n  exit $status\n)\n</code></pre>\n<p>That synchronization mechanism feels a bit convoluted; hopefully there\'s a simpler way to do it.</p>\n'}, {'answer_id': 71810273, 'author': 'RARE Kpop Manifesto', 'author_id': 14672114, 'author_profile': 'https://Stackoverflow.com/users/14672114', 'pm_score': 0, 'selected': False, 'text': "<p>No need to specify all the parameters in <code>strftime()</code> unless you really want to customize the outputting format :</p>\n<pre><code>   echo &quot;abc 123 xyz\\njan 765 feb&quot; \\\n    \\\n    | gawk -Sbe 'BEGIN {_=strftime()&quot; &quot;} sub(&quot;^&quot;,_)'\n\n    Sat Apr  9 13:14:53 EDT 2022 abc 123 xyz\n    Sat Apr  9 13:14:53 EDT 2022 jan 765 feb\n</code></pre>\n<p>works the same if you have <code>mawk 1.3.4.</code> Even on <code>awk</code>-variants without the time features, a quick getline could emulate it :</p>\n<pre><code>echo &quot;abc 123 xyz\\njan 765 feb&quot; \\\n\\\n| mawk2 'BEGIN {     (__=&quot;date&quot;)|getline _;\n                close(__)\n                        _=_&quot; &quot; } sub(&quot;^&quot;,_)'\n   \nSat Apr  9 13:19:38 EDT 2022 abc 123 xyz\nSat Apr  9 13:19:38 EDT 2022 jan 765 feb\n</code></pre>\n<p>If you wanna skip all that getline and BEGIN { }, then something like this :</p>\n<pre><code>mawk2 'sub(&quot;^&quot;,_&quot; &quot;)' \\_=&quot;$(date)&quot;\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/21564', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/156/']