Insert date/time header at top of file


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert date/time header at top of file
# 1  
Insert date/time header at top of file

I'm trying to take mrt output and put it at the top of a file along with the date and time. I was able to do it at the bottom of the file with the following

Code:
printf "********** $(date) **********\n\n" >> $OUTPUT_PATH/$HOSTNAME
mtr -r -w -c 10 $HOSTADDRESS >> $OUTPUT_PATH/$HOSTNAME
printf "\n\n\n\n" >> $OUTPUT_PATH/$HOSTNAME

I found some information on using sed to insert text at the beginning of a file and adapted my code to this

Code:
sed -i '1s/^/\n\n\n\n/' $OUTPUT_PATH/$HOSTNAME
sed -i '1s/^/MTR OUTPUT HERE/' $OUTPUT_PATH/$HOSTNAME
sed -i '1s/^/********** $(date) **********\n\n/' $OUTPUT_PATH/$HOSTNAME

This works great except for one issue. The $(date) is being output exactly as is and not showing the current date and time as it did with the printf command.

Any help on this would be appreciated.

Thanks

---------- Post updated 03-03-14 at 12:22 AM ---------- Previous update was 03-02-14 at 11:59 PM ----------

Well I was able to figure it out. All I needed to do was use double quotes for the string.

Code:
sed -i "1s/^/********** $(date) **********\n\n/" $OUTPUT_PATH/$HOSTNAME

# 2  
use double quotes instead of single quotes in sed
Code:
sed -i "1s/^/********** $(date) **********\n\n/" $OUTPUT_PATH/$HOSTNAME

# 3  
Quote:
Originally Posted by SriniShoo
use double quotes instead of single quotes in sed
Code:
sed -i "1s/^/********** $(date) **********\n\n/" $OUTPUT_PATH/$HOSTNAME

Thanks. Yep of course 5 min after I post the question I come across the answer.
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 #945
Difficulty: Medium
A Fraggle attack is a variation of the Smurf attack where an attacker sends a large amount of UDP traffic to ports 7 (Echo) and 19 (CHARGEN) to an IP broadcast address with the intended victim's spoofed source IP address.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert date/time in reoccurring blocks of lines

Hi folks, I have a little problem, that is kinda bugging me :( ... I have a logfile, that looks kinda "crippled" in the raw format ... So far, I've managed, to get it in a format like this: PRI_PTA01,2W,30,30,0,0,0,0,0,0,0,0,0,0,0,0... (5 Replies)
Discussion started by: whizzler
5 Replies

2. UNIX for Advanced & Expert Users

Insert string in binary file at top

How can i append a EBCDIC string of 100 bytes to 0th position of a binary file in UNIX. (4 Replies)
Discussion started by: param_it
4 Replies

3. Shell Programming and Scripting

insert a header in a huge data file without using an intermediate file

I have a file with data extracted, and need to insert a header with a constant string, say: H|PayerDataExtract if i use sed, i have to redirect the output to a seperate file like sed ' sed commands' ExtractDataFile.dat > ExtractDataFileWithHeader.dat the same is true for awk and... (10 Replies)
Discussion started by: deepaktanna
10 Replies

4. Shell Programming and Scripting

best way to insert a line at the top of a file?

say I want to insert "this is a test" as the first line into file A, besides echo "this is a test" > /tmp/tmpfile cat /tmp/tmpfile fileA >> /tmp/result, is there any simple way I can do it? thanks (7 Replies)
Discussion started by: fedora
7 Replies

5. Shell Programming and Scripting

Processing a log file based on date/time input and the date/time on the log file

Hi, I'm trying to accomplish the following and would like some suggestions or possible bash script examples that may work I have a directory that has a list of log files that's periodically dumped from a script that is crontab that are rotated 4 generations. There will be a time stamp that is... (4 Replies)
Discussion started by: primp
4 Replies

6. UNIX for Dummies Questions & Answers

how do I insert argument into TOP of file using vi?

when directing some text into a file can you choose where it goes like the top of the file (which is text aswell) or the middle?? if so how - especially would like to know how to do so in vi (text editor) If i were to enter an argument ($1) into a another argument ($2) would it would be... (6 Replies)
Discussion started by: rprules
6 Replies

7. Shell Programming and Scripting

insert text into top of file

how would you insert text into a existing file using aguments first arguments being the line of text and the second argument being file name (1 Reply)
Discussion started by: jimbob
1 Replies

8. UNIX for Dummies Questions & Answers

Insert date/time within a filename

Hi Guys, I need to script the renaming of files as followins: files: firstjd secondjo thirdjv My script needs to insert the date/time infront of the last 2 characters of the filenames above, any ideas greatly received :) the letters before the last 2 characters could change, I'm only... (7 Replies)
Discussion started by: cooperman
7 Replies

9. Shell Programming and Scripting

Insert Time and Date Stamp

I have a directory with following files in it ABC.000.DAT ABC.001.DAT ABC.002.DAT ABC.003.DAT I want to insert time and date stamp in file names like ABC.000.YYYYMMDDHHMM.DAT I able to insert the time and date stamp at the end of filename Kindly help (1 Reply)
Discussion started by: aajmani
1 Replies

10. Shell Programming and Scripting

SED- Insert text at top of file

Does anyone know how to insert text at the top and bottom of a file using sed? (12 Replies)
Discussion started by: MBGPS
12 Replies

Featured Tech Videos