Difference between revisions of "Bash tips"
From DarkWiki
(Created page with "===Prepending lines of text with a timestamp=== To put a date/time against each line (perhaps as part of logging), you can use the <code>ts</code> command.") |
(→Prepending lines of text with a timestamp) |
||
| Line 2: | Line 2: | ||
To put a date/time against each line (perhaps as part of logging), you can use the <code>ts</code> command. | To put a date/time against each line (perhaps as part of logging), you can use the <code>ts</code> command. | ||
| + | |||
| + | <source lang="bash"> | ||
| + | $ echo -e "foo\nbar\nbaz" | ts '[%Y-%m-%d %H:%M:%S]' | ||
| + | [2011-12-13 22:07:03] foo | ||
| + | [2011-12-13 22:07:03] bar | ||
| + | [2011-12-13 22:07:03] baz | ||
| + | </source> | ||
| + | |||
| + | If it is not already present on your system, it can be added using <code>apt</code>: | ||
| + | |||
| + | <source lang="bash"> | ||
| + | sudo apt install moreutils | ||
| + | </source> | ||
Latest revision as of 09:02, 11 December 2019
Prepending lines of text with a timestamp
To put a date/time against each line (perhaps as part of logging), you can use the ts command.
$ echo -e "foo\nbar\nbaz" | ts '[%Y-%m-%d %H:%M:%S]'
[2011-12-13 22:07:03] foo
[2011-12-13 22:07:03] bar
[2011-12-13 22:07:03] baz
If it is not already present on your system, it can be added using apt:
sudo apt install moreutils