1

I have a file which filled in streaming way line by line. I need to decrease the volume of the file by deleting the oldest record in it. I would like to count the number of lines and if the number of lines exceed than 100 then delete the oldest lines. However I got the following error:

./1.sh: line 18: syntax error near unexpected token `done'
./1.sh: line 18: `done'

Here is my code:

#!/bin/bash
FILE="11.txt"
linenum=0

while true; do

#Count number of lines
linenum=`cat "$FILE" | wc -l`

while [ $linenum -gt 100 ] do

#Delete the head of file (oldest)
sed -i 1,1d "$FILE"

#Count number of lines
linenum=`cat "$FILE" | wc -l`

done

done

Can you please help me?

4
  • This isn't safe. sed -i (effectively) makes a copy of the input file, then replaces the original with the modified copy. If the original is written to while sed is working on the copy, those changes are lost. Commented May 18, 2017 at 13:24
  • @chepner: thanks for mentioning this, do you have any idea? Commented May 18, 2017 at 13:34
  • Whatever is writing to the file needs to be responsible for trimming it. Commented May 18, 2017 at 13:46
  • @chepner: It does't work! when I applied it on a streaming file, it could't work! :(( Commented May 18, 2017 at 15:10

2 Answers 2

4

You need a linefeed or a ; between the while's condition and the do :

while [ $linenum -gt 100 ]; do

    #Delete the head of file (oldest)
    sed -i 1,1d "$FILE"

    #Count number of lines
    linenum=$(wc -l "$FILE")

done

I also indented the code properly, changed the subshell `...` notation to the more modern $(...) and removed a redundant use of cat.

Sign up to request clarification or add additional context in comments.

Comments

0

You missed the semicolon at below line

while [ $linenum -gt 100 ] do

should be

while [ $linenum -gt 100 ] ; do

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.