LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 08-04-2008, 09:13 PM   #1
DeepSeaNautilus
Member
 
Registered: Jul 2008
Posts: 65

Rep: Reputation: 15
Replacement with sed or awk


Hello I am making a daemon in shell to execute several commands depending on the date and time on the server and on the date and time I have assigned for every command to be executed. The file where I have this info looks like this:

enable min hour day month year command

1:10:11:14:2:2009:echo "Feliz dia de San Valentin!"
1:20:14:1:5:2009:echo "Feliz dia del trabajo"
1:10:19:04:08:2009:banner Daemon 'funciona!!'

If the date and time of the server is the same than the date and time of any of the registers, the command will be executed. The problem is that it will keep executing it as long as the date and time matches, Thats why I have placed an enable flag in the beginning of every register. When the command is executed it has to turn off the enable flag, I don't know how to do this in a single sed or awk instruction, can anyone please tell me?

I have tried this:
i is a variable wich has the line number of the register I want to disable.
a=`sed -n "$i,$i"p`
b=0`echo $a | cut -c 2-100`
sed s/$a/$b/g file
I just need to know how to get sed to replace the variables a and b but I don't know how to tell sed not to interpret the meta characters inside the variables.

Last edited by DeepSeaNautilus; 08-04-2008 at 09:49 PM.
 
Old 08-04-2008, 10:30 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
It sounds like you only want these run once each at the given times? wouldn't it be simpler to just use cron?
Of course, it depends on whether these are the real cmds, or just tests, as cron isn't connected to a terminal, so the echo cmd wouldn't make sense unless it was re-directed to a file.
 
Old 08-04-2008, 11:41 PM   #3
DeepSeaNautilus
Member
 
Registered: Jul 2008
Posts: 65

Original Poster
Rep: Reputation: 15
Not available

Quote:
Originally Posted by chrism01 View Post
It sounds like you only want these run once each at the given times? wouldn't it be simpler to just use cron?
Of course, it depends on whether these are the real cmds, or just tests, as cron isn't connected to a terminal, so the echo cmd wouldn't make sense unless it was re-directed to a file.
Yes it would, but the command is not available for me in the server, thatīs why I am making my own implementation of the cron command. And.. yes the echo command was a test . The real commands will be re-directed ( both stdout and stderr) to a file and also will be sent to background ( so the procces checks the time and date of every register faster ) like this:
command >& file &

Also notice that the cron command doesnīt let specify the year which I consider a limitation.

Last edited by DeepSeaNautilus; 08-04-2008 at 11:47 PM.
 
Old 08-05-2008, 01:18 AM   #4
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
Quote:
Originally Posted by DeepSeaNautilus View Post
Thats why I have placed an enable flag in the beginning of every register. When the command is executed it has to turn off the enable flag, I don't know how to do this in a single sed or awk instruction, can anyone please tell me?
As far as I can tell, you want to replace the first character of some known line with some other character. If that is so, please try something like this:

Code:
sed "$i s/^./0/" input
where input is your input file and $i is the number of the line you want to change.
 
Old 08-05-2008, 12:17 PM   #5
DeepSeaNautilus
Member
 
Registered: Jul 2008
Posts: 65

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by burschik View Post
As far as I can tell, you want to replace the first character of some known line with some other character. If that is so, please try something like this:

Code:
sed "$i s/^./0/" input
where input is your input file and $i is the number of the line you want to change.
Thanks vey much, that was exactly what I wanted.
 
  


Reply


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
awk or sed help cmontr Programming 16 05-14-2008 11:59 AM
Text replacement question: sed/awk/perl whatever BigRedBall Programming 6 02-05-2008 12:53 PM
awk and/or sed linux2man Linux - General 7 01-22-2007 11:02 AM
Sed and Awk Gins Programming 7 04-19-2006 11:32 AM
awk positional replacement qusan Programming 2 06-09-2004 11:08 AM


All times are GMT -5. The time now is 10:26 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration