Search code examples
bashunixsedsql-loaderwc

Remove three last lines from spool file using wc -l and sed


I am facing an issue that I have File that every time have different row count and it ends with sqlldr output that shows amount of rows exported to file


40968 rows selected.

Since this file will be picked up by next process I want to get rid of this additional text. So I came up with idea like this:

# Remove exported record count from end of file
remove_count=$(( $(wc -l < ${output_dir}/file.dat)-3+1 ))
echo "Removing extra lines from spooled file"
sed '$remove_count, $ d' file.dat >> file.dat

The problem is that it seems that sed cannot use variable as amount of lines after which it should start deleting. I could not find better solution. Is there something I can do better or anyone sees mistake? Thanks in advance!


Solution

  • You don't need such a complicated solution; head can do this:

    $ echo -e 'first\nsecond\nthird\nfourth' | head -n-3
    first
    

    See the manual:

           -n, --lines=[-]NUM
                  print the first NUM lines instead of  the  first  10;  with  the
                  leading '-', print all but the last NUM lines of each file