Click to See Complete Forum and Search --> : adding a timestamp to all lines of a file


maxthree
02-28-2003, 03:52 PM
Hi,

Let's say I have a script that

1. assigns timestamp to a variable
timestamp=`date '+ %y-%m-%d %H:%M:%S'`
let's say that this is set to 2003-03-02 20:51:23

2. creates a file test.tmp
contents
A
B
C
D

3. now, I want to create a new file that adds $timestamp to the contents of test.tmp

A 2003-03-02 20:51:23
B 2003-03-02 20:51:23
C 2003-03-02 20:51:23
D 2003-03-02 20:51:23

how do I do that?

red_over_blue
02-28-2003, 04:30 PM
Use sed


john@limbo ~ $ cat testfile
1
2
3
4
5
6
john@limbo ~ $ export timestamp=`date '+ %y-%m-%d %H:%M:%S'`
john@limbo ~ $ echo $timestamp
03-02-28 14:27:04
john@limbo ~ $ sed "s/$/ $timestamp/g" testfile > newtestfile
john@limbo ~ $ cat newtestfile
1 03-02-28 14:27:04
2 03-02-28 14:27:04
3 03-02-28 14:27:04
4 03-02-28 14:27:04
5 03-02-28 14:27:04
6 03-02-28 14:27:04
john@limbo ~ $

maxthree
02-28-2003, 05:04 PM
tks man / works like a charm

this will impress the hell out of my boss ...