I have a following line in the file
*/10 * * * * root /usr/lib64/sa/sa1 1 1
and */10 needs to be replaced with * preferably using sed
any inputs is appreciated
A sed command line for such a replacement would be
sed -e 's/\*\/10\b/*/'
Related
I have the output from crontab -l containing about 1500 lines.
I need to sort them based on the frequency of execution.
I tried a Perl module named Crontab::Interval but it didn't seem to accept jobs written with interval, that is * 4-20 * * *.
The goal is to identify all jobs that run more than once every hour.
Suggesting to try to filter using awk pattern matching logic.
Print only lines that: Field#1 not match * AND Field#2 not match *
crontab -l| awk '$1 !~ /\*/ && $2 !~ /\*/ 1'| sort
I have a script called script.sh which has the contents
export HELLO=$WORLD
I want to change it using a single command to
export HELLO=${WORLD:2:3}
I'm attempting to use the sed command but i'm new to linux and can't quite get it right. Here's what I have
sed -i 's/export HELLO=$WORLD/export HELLO=${WORLD:2:3}' script.sh
How should this line be written to replace the text correctly?
You just missed a / at the very end:
sed -i 's/export HELLO=$WORLD/export HELLO=${WORLD:2:3}/' script.sh
^
this / is missing
The $ is a special symbol that means end of line, also as #yvesonline points out you are missing the trailing /. This is how I would do it:
sed -i 's/\(export HELLO=\$\)\(WORLD\)/\1{\2:2:3}/' script.sh
I am looking for a script to search a line if it contains two strings and then find if the first charater of that line contains certain character and remove it.
Eg. if line contains two strings as "abc" and "xyz" it should look for first character of the line and if it contains # , it should remove it and vice-versa.
It tried to run below command in crontab and got the result
crontab -l | grep az2-er32-cxv-iz| grep aze
Output
#5,10 * * * * /opt/apps/scripts/dsm-rync -q -del -s az2-er32-cxv-iz /opt/apps/sdl/scripts/aze-dsm-rync.app.config
since , its difficult to update the crontab entry directly , i copied it to tmpfile.
crontab -l > tmpfile and tried to run sed 's/^#//' tmpfile but it is removing all # instead of the line matching with two strings
You may use gnu awk to do this easily:
awk -i inplace '/az2-er32-cxv-iz/ && /aze/{sub(/^#/, "")} 1' crontab
This will remove # from first position if line has az2-er32-cxv-iz and aze in it.
As mentioned by Shloim, I won't give you the code itself, but I'm giving you a piece of pseudo-code to start:
In order to know if a line contains at least two words, you might search for a space within that line. (grep " " <filename>)
In order to know if a line starts with a certain character, you might search for the character, followed by that one certain character. (grep "<beginning_of_line>#")
Replacing a character in a line can be done, using the sed command.
In order to add a licence to all php files, I would like to change <?php by:
<?php
/*
* Copyright (c) 2016-2017 My Project
* This file is part of My Project 1.2, more information at https://websitemyproject.com
* Licence another line
* Last line of licence
*/
So I tested with sed on one file, for the first line of the licence:
sed -i -- 's/<?php/<?php\n\/*\n * Copyright(c) 2016-2017 My Project \n *\/ /g' index.php
It works but it adds some ^M at the end of each line of the file index.php and I don't understand why.
<?php
/*
* Copyright(c) 2016-2017 My Project
*/ ^M
^M
^M
$page = 'home';^M
^M
Thank you for your help
you can insert s/\r//; to delete a carriage return chars with your sed command
sed -i -- 's/\r//;s/<?php/<?php\n\/*\n * Copyright(c) 2016-2017 My Project \n *\/ /g' index.php
Your file has Windows line endings (^M^J), your sed command is outputting Unix line endings (^J), and the result is a mixture of both.
In other words, it might look like sed is adding ^M to each line, but actually those ^M characters were already there. Your editor probably auto-detects the line-endings, and the mixed file is producing mixed results.
You can convert the line endings using a tool like dos2unix, or you can tell your sed command to output Windows style line endings by using \r\n instead of just \n.
I have a script that contains the following line:
crontab -l |sed -e 's=\(^.*/usr/local/nextone/bin/setdbrole.sh$\)=#\1=' | crontab -
which will add a '#' to a line in the crontab each time it is executed.
But everytime that line is executed comment lines are added to the crontab like the below:
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.2pG8HV installed on Tue May 12 13:02:16 2015)
so my crontab after several execution of that script get filled with hundred of comment lines.
how can I make it stop ?
Depending on your version of crontab, those lines get added at the beginning of the output of crontab -l. Ask sed to remove the two first lines then:
crontab -l | sed -e '1,2d;s=\(^.*/usr/local/nextone/bin/setdbrole.sh$\)=#\1=' | crontab -