How to add date as a prefix for each outputline


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add date as a prefix for each outputline
# 1  
Old 08-18-2010
How to add date as a prefix for each outputline

What is the best way of printing date-time at the beginning of each output line?

DTTIME=$(date +%Y%m%d%H%M%S)

Code:
myfile.log
    0.0    5.2    0.0   41.6  0.1  0.8   15.0  160.2   8  40 d0
    0.0    0.8    0.0    6.4  0.0  0.0   13.7   53.2   1   4 d2
    3.3  109.4   16.9  721.5  0.9  7.0    7.8   62.1  80 100 d3

How can I get either a static or dynamic datetime printed in front of each line? It would be nice if its a generic solution.

Code:
cat myfile.log | <sometrick>
   20100818124946  0.0    5.2    0.0   41.6  0.1  0.8   15.0  160.2   8  40 d0
   20100818124946  0.0    0.8    0.0    6.4  0.0  0.0   13.7   53.2   1   4 d2
   20100818124946  3.3  109.4   16.9  721.5  0.9  7.0    7.8   62.1  80 100 d3

# 2  
Old 08-18-2010
One way:
Code:
awk -v DTTIME=$(date +%Y%m%d%H%M%S) '{print DTTIME, $0}' myfile.log

# 3  
Old 08-18-2010
Thaks that works,,
On my solaris 8 I have to use /usr/xpg4/bin/awk instead of /bin/awk for inline variable declarations to work i.e . awk -v <var declaration>
# 4  
Old 08-18-2010
sed

Code:
sed "s/^/$(date +%Y%m%d%H%M%S) /" myfile.log

# 5  
Old 08-18-2010
Quote:
Originally Posted by frans
Code:
sed "s/^/$(date +%Y%m%d%H%M%S) /" myfile.log

The date output in sed will keep changing in seconds.

Not sure if this is the original request.
# 6  
Old 08-19-2010
Quote:
Originally Posted by rdcwayx
The date output in sed will keep changing in seconds.
Ooops !
with a fixed date in a variable
Code:
D=$(date +%Y%m%d%H%M%S); sed "s/^/$D /" myfile.log

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add the word "prefix" to beginning of line using sed

SUSE linux bash shell this works test -d /tmpp && echo "directory exists" || echo "directory doesn't exists" |sed -e "s/^/prefix /" prefix directory doesn't exists but why doesn't this work? test -d /tmp && echo "directory exists" || echo "directory doesn't exists" |sed -e... (3 Replies)
Discussion started by: snoman1
3 Replies

2. Shell Programming and Scripting

Bash to add unique prefix to extracted zip folder

In the bash below in each .zip there is a folder to be extracted Variants that I am trying to make unique by adding the prefix, before the _ from the .zip. The script does execute, but the prefix is not added to the extracted folder. Rather the Variants folder is added to each file within it. Thank... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

Need to add prefix using sed or awk from cat the file

I need the use sed or AWK using cat the file Node1 TDEV RW 1035788 TDEV RW 1035788 Server1 TDEV RW 69053 Server2 TDEV RW 69053 TDEV RW 103579 Server3 TDEV RW 69053 server4 RDF1+TDEV RW 69053 RDF1+TDEV RW 517894 RDF1+TDEV RW 621473 server6 TDEV RW 34526 TDEV RW 34526 (22 Replies)
Discussion started by: ranjancom2000
22 Replies

4. Shell Programming and Scripting

Extract Uniq prefix from a start and end prefix

Dear All, assume i have a file with content: <Start>6000</Start> <Stop>7599</Stop> the output is: 6000 7000 7100 7200 7300 7400 7599 how should we use any awk, sed, perl can do this task, means to extract the uniq prefixes from the start and stop prefix. Thanks Jimmy (3 Replies)
Discussion started by: jimmy_y
3 Replies

5. UNIX for Dummies Questions & Answers

Add foldername as prefix to files

Hi there, I am looping through folders in order to rename the files in the current folder. So, given me being a newbie here, I would do that with a for-loop. The renaming per se should be like this: I want to add the folder-name as a prefix to the files in the folder in question. For instance,... (6 Replies)
Discussion started by: jahndavik
6 Replies

6. Shell Programming and Scripting

Need to add a date column (today's date) in file

Hi I have file with number status and date1 and date1 field, want add a column today between column date1 and date2. file1.txt number status date1 date2 ===== ==== === ===== 34567 open 27/06/13 28/06/13 45678 open 27/06/13 28/06/13 43567 open 27/06/13 28/06/13 ... (1 Reply)
Discussion started by: vijay_rajni
1 Replies

7. UNIX for Dummies Questions & Answers

LINUX - How to add a prefix to a set of files

Hi All, I have a file (lets say filenames.txt) which contains a list of file names. Is there a way I can append a prefix to the filenames as below, Thanks much for your help Freddie (3 Replies)
Discussion started by: dsfreddie
3 Replies

8. Shell Programming and Scripting

Need awk script to add a prefix to each line in file

Hello , I have file with below content : '165567885', '165568443', '165568805', I need an awk script that would add a prefix zero after first ' . Like '0165567885', '0165568443', '0165568805', Please help. Thanks in advance. (5 Replies)
Discussion started by: rmv
5 Replies

9. Shell Programming and Scripting

Extract date from file header and prefix it to all lines

Hello All, I have a file in the following format. I want to extract the date(020090930, 020090929) in the string "STPAGE020090930" and "STPAGE020090929" and prefix it to all lines below them. The output must be put into a new file. STPAGE020090930 xyzz aalc... (3 Replies)
Discussion started by: john2022
3 Replies

10. Shell Programming and Scripting

Prefix the current date and time to the output of ps

Hi, I need to write a script, that will take the current date, time, and the output from # ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm and spit it to a file, so it'll look like this... PID TID CLS RTPRIO NI PRI PSR %CPU STAT WCHAN COMMAND 1 1 TS... (2 Replies)
Discussion started by: Bloke
2 Replies
Login or Register to Ask a Question