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.
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):
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:
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.
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.
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)