-   Programming (
-   -   Using sed in a shell script to add date and time (

seefor 02-24-2009 10:48 PM

Using sed in a shell script to add date and time
:scratch: 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
        let COUNT=COUNT+1
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.

syg00 02-24-2009 11:42 PM

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.

slackert 02-24-2009 11:42 PM

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|"

seefor 02-25-2009 01:21 PM



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

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

That worked like a charm :)

Thanks for all the help!

All times are GMT -5. The time now is 04:36 AM.