I'm trying to append the current date and time to a log file every minute using cron. I want the date and time to be formatted in a specific way.
This works:
* * * * * date >> /home/user/time1.txt
This doesn't:
* * * * * date +%Y%m%d%H%M%S >> /home/user/time2.txt
Any insight is much appreciated!
The problem is that cron treats % as newlines. You need to escape them
From crontab POSIX man page:
Percent-signs (%) in the command, unless escaped with backslash \,
will be changed into newline characters, and all data after the first % will be
sent to the command as standard input.
* * * * * date +\%Y\%m\%d\%H\%M\%S >> /home/user/time2.txt
escape % with \ and then it should work.
Related
How can I search for the following string in vim:
\$5 * \$5 * \$6 * 10000
Just typing
/\$5 * \$5 * \$6 * 10000
in command mode doesn't do the job.
Thanks
Put a \ before \ (double \\), $, and *.
/\\\$5 \* \\\$5 \* \\\$6 \* 1000\\\$5 \* \\\$5 \* \\\$6 \* 10000
I am new bee for shell script. Please redirect me to correct post if this question is redundant.
I have file.txt which has text like below and want to change the 2nd row in file
Property1=0
Property2=0 50 14 1/1 * ? *
Property3=0
I want to replace 2nd line to change 50 14 to current time, file will look like
Property1=0
Property2=0 58 15 1/1 * ? *
Property3=0
and next time it will look like
Property1=0
Property2=0 03 16 1/1 * ? *
Property3=0
Please help me how to change 2nd line.
You can use sed to change the second line
sed '2s/.*/Property2=0 58 15 1\/1 * ? */' test
It seems like you're trying to increment, but I don't see the pattern in the third example
This is probably easier done using awk than sed.
awk -v min=$(date +%M) -v hour=$(date +%H) 'NR == 2 { $2 = min; $3 = hour } 1' file > file.new
The two date commands set awk variables to the current time. NR == 2 matches line 2 in the file, then it replaces the 2nd and 3rd fields with those time variables. 1 at the end causes the current line to be printed.
I have to replace a specific line of a file with some other value. for eg: I have a file with content as below:
request.timing=0/10 * * * * * ?
Now, I want to replace 10 (or to be specific, any value in that place) with a dynamic value. Eg, if I want to replace that with 20, the file should be updated as:
request.timing=0/20 * * * * * ?
Can someone help me? I am using sed as below:
sed -i "s/request.timing=0\/??/request.timing=0\/$poller"
where poller is dynamic value we pass.
First of all, you must check the characters the variable $poller has, because sed can interpret any character in there as a special one. Once you have checked that, use a sed separator which is not inside $poller. Let's suppose we can use /:
sed -r 's/(request.timing=0\/)[0-9]{2}/\1'"$poller"'/g'
How does it works
-r option activates extended regular expressions so you can use things like [0-9]{2}, and with ( ... ) you're capturing the string to use in the replacement site.
$ echo 'request.timing=0/10 * * * * * ?' |
sed -r 's:(request.timing=0/)[0-9]+:\1135:'
request.timing=0/135 * * * * * ?
$ poller="135"
$ echo 'request.timing=0/10 * * * * * ?' |
sed -r 's:(request.timing=0/)[0-9]+:\1'"$poller"':'
request.timing=0/135 * * * * * ?
The reason that you were having troubles is that ? is not valid in the regular expression. Use . instead. ? is used to say the previous character may be present 0 or 1 times.
sed -i "s/request.timing=0\/../request.timing=0\/$poller"
(That's not to say the other answers aren't correct!)
What I try to accomplish with 'sed' is to remove a line if the pattern match.
and adding a line if pattern doesn't match.
But how could I make this?
Thanks in advance
Example:
I want this to use this line in Crontab:
*/1 * * * * script 1 test
With a other script I want to remove this line if this pattern is there, and otherwise, if pattern is not matching then add this line.
awk -v tgt='*/1 * * * * script 1 test' 'index($0,tgt){found=1;next} {print} END{if (!found) print tgt}' file
A good practice is to tag your automatically added crontab entries with a comment:
* * * * * script 1 test #projectfoo-testjob
This allows you to easily remove the job:
sed -i.bak '/projectfoo-testjob/d' file
or add it:
grep -q "projectfoo-testjob" file || \
echo '*/1 * * * * script 1 test #projectfoo-testjob' >> file
By tagging the job like this, you prevent duplicates when the user reformats their crontab e.g. to use tabs instead of spaces, and you prevent deleting similar jobs that the user added for themselves.
This might work for you (GNU sed):
sed 'x;/./{x;b};x;/pattern/{h;d};$a\append new line' file
Only append line if not found in file using grep -qF 'line' file && echo 'line' >> file:
grep -qF '*/1 * * * * script 1 test' file || echo '*/1 * * * * script 1 test' >> file
When I read[command] some lines including character '*', it seems that '*' will be looked as a wildcard. whether exsits some solutions leting '*' just be a '*', please!
It depends how you use the variable: if you quote it, filename expansion will not happen. Example:
$ ls
f1 f2 f3
$ read line
*
$ echo "$line"
*
$ echo $line
f1 f2 f3
If you do not want any of the special file name characters to be used as wildcards then enter the following in your script before the read.
set -o noglob
This will prevent the * ? and [] from having special meaning and treat them as normal characters.
The following example demonstrates the point
touch 1 2 3
echo "With wild card expansion"
echo *
echo "Without wild card expansion"
set -o noglob
echo *
And produces the following results
With wild card expansion
1 2 3
Without wild card expansion
*
You can escape it with the escape character: \*. This means the * will be a literal *, not matching one or more characters as the glob pattern.