LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Using sed in a shell script to add date and time (http://www.linuxquestions.org/questions/programming-9/using-sed-in-a-shell-script-to-add-date-and-time-707310/)

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.

Code:

#!/bin/bash
today=date +"%D"
hour=date +"%T"
for y in 205 206 207 208
do
COUNT=1
while [ $COUNT -lt 253 ]; do
snmpget -v 2c -Ovq -r 0 -c public 1.1.$y.$COUNT 1.3.6.1.4.1.9986.3.5.2.1.3.0 SNMPv2-SMI::enterprises.9986.3.5.2.6.2.2.5.4.1.2.1.1.1.1 SNMPv2-SMI::enterprises.9986.3.5.2.6.2.2.5.2.1.2.1.1.1 1.3.6.1.4.1.9986.3.5.2.6.2.2.5.1.1.17.1.1 1.3.6.1.4.1.9986.3.5.2.6.2.2.5.1.1.13.1.1 1.3.6.1.4.1.9986.3.5.2.6.2.2.5.1.1.14.1.1 1.3.6.1.4.1.9986.3.5.2.6.2.2.5.1.1.11.1.1 1.3.6.1.4.1.9986.3.5.2.6.2.2.5.1.1.12.1.1 1.3.6.1.4.1.9986.3.5.2.6.2.2.5.1.1.32.1.1 SNMPv2-SMI::enterprises.9986.3.5.2.3.2.0 | sed -n '1h;2,$H;${g;s/\n/;/g;p}' >> data.xls
        let COUNT=COUNT+1
done
done
sed -i 's/$/$today;$hour/' data.xls

I thought that
Code:

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.
SeeFor

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.

Code:

#!/bin/bash

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

Code:

#!/bin/bash

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 11:38 PM.