writing the timestamp to as a header in a file

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting writing the timestamp to as a header in a file
# 1  
Network writing the timestamp to as a header in a file

hello mates,

this is my first post. please help me out. i have got a file with some data in it. i am asked to write the timestamp as a header for that file. i mean the time the file created should be mentioned at the top of the file. i know we can use sed to insert a sentence but don't know how to insert the date there. please help me out. its a matter of urgency.

# 2  
Depends a bit on a few things. If you have a command foo let's say, that writes data to standard out, and you are capturing the data and want it timestamped, then something like this (assuming you are using ksh or bash):

(date; foo) >output-file

This will write the date and then the output from foo to the named file. You can give any parameters to date and/or foo that are needed.

If your file already exists, and you want to add the current time (assuming it was created by a previous command so current time is nearly the creation date of the data), and the above won't work because maybe the command creates the file and doesn't write to standard ouput, then something like this might work:

(date; cat orig-file) >new-file && mv new-file orig-file

This will create a new file with the date, add the contents from the original file, and if that operation was successful, will move the new file 'on top' of the original file.
# 3  

Fantastic mate. i was looking for the second option and it works out for me. thanks a lot. but when i use the mv command, it is asking me whether to move it or not. can't we stop the shell asking this question and mv the file name automatically. may be i may have to write a script for this. isn't it? i am new to unix and not upto the standards of scripting. just a novice. any how thanks for the quick reply mate. if possible could you please explain the command. it works but i don't understand it properly.
# 4  
Use -f option

man mv

-f, --force
do not prompt before overwriting
This User Gave Thanks to ahamed101 For This Post:
# 5  
agama and ahamed101, thank you both. -f tip is very useful as well.

# 6  
Originally Posted by jdsony
if possible could you please explain the command. it works but i don't understand it properly.
The parentheses cause all of the commands to be run in a sub-shell. As such, the standard output from all of the commands goes to the same place so (command1; command2) >file nicely writes all of the output with the header line as you wanted. The && causes the next command on the line to be executed only if the previous command (the sub-shell) executed and returned a good return code. Thus, the move command is run only if the tmp file was successfully created. This prevents overlaying the original file with potentially bad data in the tmp file if the command failed. This is important if the data is difficult to, or cannot be, recreated.

Hope this helps you understand.
# 7  
thanks for the explanation mate. now it makes sense to me. thanks a lot.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #337
Difficulty: Easy
:set number in vi will add line numbers to the file and automatically save to disk.
True or False?

10 More Discussions You Might Find Interesting

1. Programming

Node-RED: Writing MQTT Messages to MySQL DB with UNIX timestamp

First, I want to thank Neo (LOL) for this post from 2018, Node.js and mysql - ER_ACCESS_DENIED_ERROR I could not get the Node-RED mysql module to work and searched Google until all my links were purple! I kept getting ER_ACCESS_DENIED_ERROR with the right credentials. Nothing on the web was... (0 Replies)
Discussion started by: Neo
0 Replies

2. Shell Programming and Scripting

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
3 Replies

3. Shell Programming and Scripting

Python DictWriter header - not writing in first line of existing file

Hello I am facing a very unique problem and not able to understand why. I have written a function which will check header of the file. If header is present good else it will write the header on top def writeHeaderOutputCSV(fileName): # See if the file exist already try: ... (0 Replies)
Discussion started by: radioactive9
0 Replies

4. Shell Programming and Scripting

To check timestamp in logfile and display lines upto 3 hours before current timestamp

Hi Friends, I have the following logfile. Currently time in india is 07/31/2014 12:33:34 and i have the following content in logfile. I want to display only those entries which contain string 'Exception' within last 3 hours. In this case, it would be the last line only I can get the... (12 Replies)
Discussion started by: srkmish
12 Replies

5. UNIX for Dummies Questions & Answers

Merge all csv files in one folder considering only 1 header row and ignoring header of all others

Friends, I need help with the following in UNIX. Merge all csv files in one folder considering only 1 header row and ignoring header of all other files. FYI - All files are in same format and contains same headers. Thank you (4 Replies)
Discussion started by: Shiny_Roy
4 Replies

6. UNIX for Dummies Questions & Answers

How to compare a file by its timestamp and store in a different location whenever timestamp changes?

Hi All, I am new to unix programming. I am trying for a requirement and the requirement goes like this..... I have a test folder. Which tracks log files. After certain time, the log file is getting overwritten by another file (randomly as the time interval is not periodic). I need to preserve... (2 Replies)
Discussion started by: mailsara
2 Replies

7. Shell Programming and Scripting

Getting a relative timestamp from timestamp stored in a file

Hi, I've a file in the following format 1999-APR-8 17:31:06 1500 3 45 1999-APR-8 17:31:15 1500 3 45 1999-APR-8 17:31:25 1500 3 45 1999-APR-8 17:31:30 1500 3 45 1999-APR-8 17:31:55 1500 3 45 1999-APR-8 17:32:06 1500 3 ... (1 Reply)
Discussion started by: vaibhavkorde
1 Replies

8. Shell Programming and Scripting

Comparing one file header with another file header

Hi Experts, In our project we have requirement where in we have to compare header of one file with header in the parameter file. There are 20 files which we ftp from one site. All this files have different header. We are comapring this file with our parameter file(which is having the header... (2 Replies)
Discussion started by: Amey Joshi
2 Replies

9. Shell Programming and Scripting

Change File Header Timestamp

Please I am new to Unix and this simple question I am already answered but struggling to find the answer. I have a data file which contains header record which conatins date timestamp. I need to find a way of simply updating the date time stamp to current date timestamp. So if the header... (5 Replies)
Discussion started by: mudi
5 Replies

10. Linux

Reading the header of a tar file(posix header)

say i have these many file in a directory named exam. 1)/exam/newfolder/link.txt. 2)/exam/newfolder1/ and i create a tar say exam.tar well the problem is, when i read the tar file i dont find any metadata about the directories,as you cannot create a tar containig empty directories. on the... (2 Replies)
Discussion started by: Tanvirk
2 Replies

Featured Tech Videos