user3721640
user3721640

Reputation: 947

script to clear file contents using sed

I am trying to delete few lines (that are 1 day older) from a file using sed but it gives an error while executing the script.

What could be causing the above error? Could anyone please help?

~]# ./test.sh

Jan 20
36
sed: -e expression #1, char 3: unexpected `,'

Here is the script:

month=$(date --date="1 day ago" | cut -d " " -f2,3)

echo $month

line=$(grep -n "$month" test.log | cut -d : -f 1 | tail -1)

echo $line

if [ ! -z "$line" -a "$line" != " " ];

then

sed -i '1,"$line"d' test.log

#echo "sed -i '1,"$line"d' test.log"

else

exit

fi

Upvotes: 0

Views: 123

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174786

I suggest you to change the sed line as,

sed -i '1,'"$line"'d' test.log
          ^       ^        
          |       |

Upvotes: 3

Related Questions