adding lines at special place in crontab


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting adding lines at special place in crontab
# 1  
Old 05-19-2005
adding lines at special place in crontab

Hi ,

i export the crontab in a file (i've no root right) and i would add lines from a file at a special place and rewrite the output in an another file.

the special place is as this :

45 04 * * * /home/toto.sh > /dev/null 2>&1
#

so i would search for toto.sh and insert the lines , the line after the "#"

as this :


45 04 * * * /home/toto.sh > /dev/null 2>&1
#
00 05 * * * /home/neslines.sh > /dev/null 2>&1
#


thanks ina dvance

Christian
# 2  
Old 05-20-2005
Code:
$ more footab
45 04 * * * /home/other.sh > /dev/null 2>&1
#
45 04 * * * /home/toto.sh > /dev/null 2>&1
#
45 04 * * * /home/another.sh > /dev/null 2>&1
#
$ more dotab.awk
/toto.sh/ {
  print
  printf( "#\n%s\n#\n", "00 05 * * * /home/neslines.sh > /dev/null 2>&1" )
}
{ print }
$ nawk -f dotab.awk footab
45 04 * * * /home/other.sh > /dev/null 2>&1
#
45 04 * * * /home/toto.sh > /dev/null 2>&1
#
00 05 * * * /home/neslines.sh > /dev/null 2>&1
#
45 04 * * * /home/toto.sh > /dev/null 2>&1
#
45 04 * * * /home/another.sh > /dev/null 2>&1
#

Cheers
ZB
# 3  
Old 05-20-2005
Thanks for your help ,

well it's quite i wanted but not completly :

i would insert the content of a file i.e many lines and i don't want to reinsert the line before , here the 'toto.ksh' line !

regards

Christian
# 4  
Old 05-21-2005
The reinsertion of the "toto.sh" line was sloppiness on my part - apologies Smilie
Code:
$ more footab
45 04 * * * /home/other.sh > /dev/null 2>&1
#
45 04 * * * /home/toto.sh > /dev/null 2>&1
#
45 04 * * * /home/another.sh > /dev/null 2>&1
#
$ more dotab.awk
{
  if ( $0 ~ /toto.sh/ ) {
    print
    printf( "#\n%s\n",
      "00 05 * * * /home/neslines.sh > /dev/null 2>&1" )
  } else {
    print
  }
}
$ awk -f dotab.awk footab
45 04 * * * /home/other.sh > /dev/null 2>&1
#
45 04 * * * /home/toto.sh > /dev/null 2>&1
#
00 05 * * * /home/neslines.sh > /dev/null 2>&1
#
45 04 * * * /home/another.sh > /dev/null 2>&1
#

Anyway, if I understand your requirements correctly, then the following Perl code will do the trick....
Code:
$ more footab
45 04 * * * /home/other.sh > /dev/null 2>&1
#
45 04 * * * /home/toto.sh > /dev/null 2>&1
#
45 04 * * * /home/another.sh > /dev/null 2>&1
#
$ more moretab
01 04 * * * /home/1.sh > /dev/null 2>&1
#
02 04 * * * /home/2.sh > /dev/null 2>&1
#
03 04 * * * /home/3.sh > /dev/null 2>&1
#
$ more dotab.pl
#!/usr/bin/perl

open( INFILE, "footab" );
@lines=<INFILE>;
close( INFILE );

foreach (@lines) {
  print $_;
  if ( $_ =~ /toto\.sh/ ) {
    $found_toto = 1;
  }
  if ( $found_toto == 1 && $_ =~ /#/ ) {
     open( MOREFILE, "moretab" );
     print $_ while( <MOREFILE> );
     close( MOREFILE );
     $found_toto = 0;
  }
}
$ ./dotab.pl
45 04 * * * /home/other.sh > /dev/null 2>&1
#
45 04 * * * /home/toto.sh > /dev/null 2>&1
#
01 04 * * * /home/1.sh > /dev/null 2>&1
#
02 04 * * * /home/2.sh > /dev/null 2>&1
#
03 04 * * * /home/3.sh > /dev/null 2>&1
#
45 04 * * * /home/another.sh > /dev/null 2>&1
#

Cheers
ZB
# 5  
Old 05-22-2005
Could use sed...
Code:
$ cat prog.sed
/toto.sh/{n;r moretab
;}

$ sed -f prog.sed footab
45 04 * * * /home/other.sh > /dev/null 2>&1
#
45 04 * * * /home/toto.sh > /dev/null 2>&1
#
01 04 * * * /home/1.sh > /dev/null 2>&1
#
02 04 * * * /home/2.sh > /dev/null 2>&1
#
03 04 * * * /home/3.sh > /dev/null 2>&1
#
45 04 * * * /home/another.sh > /dev/null 2>&1
#

# 6  
Old 05-23-2005
Thanks both of yours , the 2 methods work fine !

It's a pleasure , i'm progressing everyday !

Christian
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Special column adding

Hi Team, I would like to add two dummy columns at the end of the file. I know how to add the columns using awk . Here is my problem. we have bit strange delimiter when I od -bc it shows 244 as its binary value. 0002200 060 244 060 244 060 244 066 065 244 060 244 244 012 0... (1 Reply)
Discussion started by: gvkumar25
1 Replies

2. Shell Programming and Scripting

Help with re-organize data by adding "-" in empty place

Input file data_1 data_2 A B C data_1 data_2 B D data_1 data_2 A C data_1 data_2 A data_1 data_2 B C data_1 data_2 C data_1 data_2 A B data_1 data_2 data_1 data_2 C B A . . Output file data_1 data_2 A B C data_1 data_2 - B D data_1 data_2 A - C (3 Replies)
Discussion started by: perl_beginner
3 Replies

3. Shell Programming and Scripting

merging files and adding special columns

Hi everyone, I got a problem with merging files and hoped one of you would have an idea how to approach this issue. I tried it with awk, but didn't get far. This is what I have: I got 40 files looking like the ones below. All have three columns but the number of rows differs (20000 to 50000).... (6 Replies)
Discussion started by: TuAd
6 Replies

4. Shell Programming and Scripting

How to get awk to edit in place and join all lines in text file

Hi, I lack the utter fundamentals on how to craft an awk script. I have hundreds of text files that were mangled by .doc format so all the lines are broken up so I need to join all of the lines of text into a single line. Normally I use vim command "ggVGJ" to join all lines but with so many... (3 Replies)
Discussion started by: n00ti
3 Replies

5. Shell Programming and Scripting

Adding new lines to a file + adding suffix to a pattern

I need some help with adding lines to file and substitute a pattern. Ok I have a file: #cat names.txt name: John Doe stationed: 1 name: Michael Sweets stationed: 41 . . . And would like to change it to: name: John Doe employed permanently stationed: 1-office (7 Replies)
Discussion started by: hemo21
7 Replies

6. Shell Programming and Scripting

Insert a special character $ in place of extra spaces

Hi Experts, I have called some.txt with the following content. oracle HYRDSRVIHUB01 pts/0 TESTIHUB 07-JUN-10 CREATE TABLE TESTIHUB PHONE ... (12 Replies)
Discussion started by: naree
12 Replies

7. Shell Programming and Scripting

Adding text to file on certain lines with(special characters)

I need to add "new lines" of text with special characters, to specific lines in the file. There are 3 modifications needed. Been testing 2 here without success. #!/usr/bin/perl use FileHandle; $file=FileHandle->new; $FILENAME="/opt/etc/usr/file.txt"; $file->open ("<$FILENAME") or die... (13 Replies)
Discussion started by: A4ron4perl
13 Replies

8. Shell Programming and Scripting

Find a string and place two blank lines

Hi friends, I am looking for a line to find a particular string in my file and once found then replace with 2-3 blank lines before the string Example: aaa 11 bbb 1 2 3 aaa 22 bbb 4 5 6 Output (4 Replies)
Discussion started by: shaliniyadav
4 Replies

9. Shell Programming and Scripting

Adding a special character at the end of the line

I used following to add * at the end of the line in file1. It adds * at the end but has a space before it for some lines but some other lines it adds exactly after the last character. How do I take out the space ? sed 's/$/*/' file1 > file2 example: contents of file1 : ... (2 Replies)
Discussion started by: pitagi
2 Replies

10. Shell Programming and Scripting

Place a comma on lines

Is it possible to place a comma in the desired places, like 10spaces after or 15 spaces after, irrespective of the contents??? Ex:File: TEST TEST: vimalthomaswants to place a comma can he do it in the desired places? as per the above file, i need to place a comma after 10th space... (4 Replies)
Discussion started by: vj8436
4 Replies
Login or Register to Ask a Question