LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 02-24-2009, 10:48 PM   #1
seefor
Member
 
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.

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
 
Old 02-24-2009, 11:42 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,153

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
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
slackert
LQ Newbie
 
Registered: Dec 2007
Posts: 6

Rep: Reputation: 1
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|"
 
Old 02-25-2009, 01:21 PM   #4
seefor
Member
 
Registered: Mar 2006
Posts: 34

Original Poster
Rep: Reputation: 15
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!
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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