I'm using the following bash script to delete all but the last 'x' lines in a log file:
#!/bin/bash # Script to write to the log file every minute and delete all but the last # set time in hours echo `date +\%Y\%m\%d\%H\%M\%S`,`/home/pi/temp/temp` >> /home/pi/temp/temp.log HOURS="12" #Nr of hours in the log LINES=$(( $HOURS * 60 + 1 )) #Nr of minutes/lines in the log echo $LINES #Included just for testing if i'm getting the desired amount of lines sed -e :a -e '$q;N;$LINES,$D;ba' temp.log >temp1.log ;mv temp1.log temp.log
I substituted the $LINES for its value (12 hours), 721 and the script is working as a cron task, runs every minute, without errors, so i'm sure the error is in the part that includes the variable in the sed.
Also, I just copied this from some website, if someone could be kind enough to explain me what is going on in the sed command, maybe i'd be able to figure out the mistake myself.
This is the error:
sed: -e expression #2, char 8: extra characters after command.
Also, it deletes all the lines in the file.
You need to take the variables out of single quotes:
sed -e :a -e '$q;N;'"$LINES"',$D;ba'
%is not a special character for the shell, you don't need to escape it:
$(...)instead of backticks: easier to see and way easier to nest.
sed can edit a file in-place, you don't need to write to a temp file and
sed -i -e :a -e '$q;N;'"$LINES"',$D;ba'
this is very similar to this stackoverflow answer that is well documented. Read that for insight.
Local articles referenced by this article: