Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-24-2009, 10:48 PM   #1
Registered: Mar 2006
Posts: 34

Rep: Reputation: 15
Using sed in a shell script to add date and time

No clue if I'm doing this right, but what I'm trying to accomplish is add the current date and time to each line of the output.

today=date +"%D"
hour=date +"%T"
for y in 205 206 207 208
while [ $COUNT -lt 253 ]; do
snmpget -v 2c -Ovq -r 0 -c public 1.1.$y.$COUNT SNMPv2-SMI::enterprises.9986. SNMPv2-SMI::enterprises.9986. SNMPv2-SMI::enterprises.9986. | sed -n '1h;2,$H;${g;s/\n/;/g;p}' >> data.xls
sed -i 's/$/$today;$hour/' data.xls
I thought that
sed -i 's/$/$today;$hour/' test
would work but it only adds $today;$hour to the file.

Any help would be greatly appreciated

Thanks in advance.
Old 02-24-2009, 11:42 PM   #2
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,815

Rep: Reputation: 2180Reputation: 2180Reputation: 2180Reputation: 2180Reputation: 2180Reputation: 2180Reputation: 2180Reputation: 2180Reputation: 2180Reputation: 2180Reputation: 2180
If you use double quotes rather than single, sed will allow variable substitution. I doubt you'll like the result much more though.
Have you attempted to run that script ??? - I can't believe the assignments for today and hour will work.

And if you can get them to do what you appear to want, the slashes in the $today field will upset sed monumentally - try +%F instead maybe.
Old 02-24-2009, 11:42 PM   #3
LQ Newbie
Registered: Dec 2007
Posts: 6

Rep: Reputation: 1
See if this works for you.


TODAY=$(date +%D)
HOUR=$(date +%T)

echo $TODAY
echo $HOUR

LINE="A line of text."

echo "LINE before sed: $LINE"

echo -n "LINE after sed: "

echo $LINE | sed -e "s|$| $TODAY $HOUR|"
Old 02-25-2009, 01:21 PM   #4
Registered: Mar 2006
Posts: 34

Original Poster
Rep: Reputation: 15

TODAY=$(date +%D)
HOUR=$(date +%T)

sed -i "s|$|$TODAY;$HOUR|" test

That worked like a charm

Thanks for all the help!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script - Date/Time runnerpaul Programming 9 08-20-2008 07:45 AM
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 09:59 AM
how to add date n time in proxy access.log sunlinux Linux - Networking 3 08-23-2007 06:54 AM
Using sed in a shell script RobHill Linux - General 4 05-29-2007 03:31 PM
Script add date to filename amphion Linux - Newbie 2 06-02-2004 07:12 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:47 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration