Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

append text with date to a file

Status
Not open for further replies.

dbadmin

Programmer
Jan 3, 2003
147
US
Hi,

I have a text file to which I need to append a line with current date (mm/dd/yyyy). How could I do it? I know appending line could be done using SED like a command as follows

1a\
Text1 to be added\
Text2 to be added

which will add two lines after the first line in the file. Is there any way I could call the date to these added strings?

Thanks,
dbadmin
 
Enclose your required date construct in ` (backticks, not apostrophes). Or have I misunderstood?
 
or you can append a single line with

echo `date +%m/%d/%Y` >> file_to_be_appended

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Hi KeThanks for the quick reply. I tried this
my sed command

1a\
Text1 `date +%m%d%y` to be added\
Text2 to be added

That too doesn't work. It prints to the file the same text.

Thanks,
dbadmin
 
sed '1a\
Text1 '"`date +%m%d%y`"' to be added\
Text2 to be added
' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi PHV,

I tried that too. It does the same thing puts the same text in the file

Text1 '"`date +%m%d%y`"' to be added

Thanks,
dbadmin
 
Any chance you could post the code launching the sed command and which shell you use ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi,

The sed commands does these things for me
1. deletes a line with create word on it from temp.txt
2. add two lines after the 9th line into temp.txt

I need to achive this
a) In the one of the line added I need to put the user who added it and the date in the respective places.

My plan was to pass the shell variables (for date and user) to sed. I don't know how to do it.

My command.sed file looks like this now

/create/d
9a\
#The argument arg1 is added on '"`date +%m%d%y`"' by user for New requirement\
arg1=master

I am calling this command.sed from a korn shell script which is like this

sed -f command.sed /users/temp.txt > /users/temp1.txt

Thanks,
dbadmin

 
if the script is called by the user, then `logname` should work for the usename

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Either inline sed commands:
sed '/create/d
9a\
#The argument arg1 is added on '"$(date +%m%d%y) by $(logname)"' for New requirement\
arg1=master
' /users/temp.txt > /users/temp1.txt

Or kind of parametized sed script:
/create/d
9a\
#The argument arg1 is added on @date@ by @user@ for New requirement\
arg1=master

and the shell:
sed -f command.sed /users/temp.txt | sed "s!@date@!$(date +%m%d%y)!;s!@user@!$(logname)!" > /users/temp1.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top