Replace part of line

* * * * * 

on

 */15 * * * * 

(yes, this is cron - so that bad users do not launch too often).

The first value assigned to the variable abuse, the second variable of the norms.

But no matter how I fought, I don’t get an exact line match.

I tried:

 abuse="\* \* \* \* \*" abuse2='\* \* \* \* \*' abuse3='* * * * *' abuse4="* * * * *" 

The first two examples give the string in this form, i.e. with escaping characters, the second two give a list of all file folders.

Respectively and

 find -type f -exec sed -i /var/spool/cron/ 's/$ABUSE/$NORM/g' {} \; 

not working, giving out

extra characters after command

Tell me the solution, please.

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

try this:

 $ abuse='\* \* \* \* \*' $ norm='*/15 * * * *' $ sudo find /var/spool/cron/ -type f -exec sed -i "s,$abuse,$norm," {} + 

errors you have noticed:

  1. The path you put is clearly in the wrong place of the team.
  2. if in the arguments of the s command of the sed program, for example, the symbol / is used , then the same symbol, of course, cannot serve to separate the arguments. I used the symbol , as clearly not included in any of the arguments.
  • Thank you, it works. - Jurgen Semikoff
  • if you are given an exhaustive answer, please tick it accepted ("tick" to the left of the answer). - aleksandr barakin

Variables in bash are only inserted in double quotes. Replace

  's/$ABUSE/$NORM/g' 

on

  "s/$ABUSE/$NORM/g" 
  • Declaring a variable in which of the options will be true? Because when using ABUSE = '* * * * *' I get the error sed: -e expression # 1, char 8: extra characters after command - Jurgen Semikoff
  • problem with a slash inside norm. There are two solutions - to escape this slash or replace the slashes in the sed command with another character, as in the answer by alexander barakin - andrybak