We know how to truncate a table in SQL,i.e. retain the structure of the table removing the contents of the table.
In UNIX ,lets say we want to remove the contents of the file and retain the empty file (zerobyte file). How can we achieve this.
➡ One way is to delete the file completely and recreate it.
➡The second method is to open the file using an editor such as vi and delete each line and save.
But what if we want to truncate the file on the fly?
Consider the problem statement:
Check if the file called dataflow has been sent by upstream and count the number of records. If records exceed threshold (value of 10,000) truncate the file and FTP it to the remote server indicating threhold breach-incorrect records. Else FTP the file as it is.
UNIX has a command for this:
We can use
cat >dev >null dataflow
This would make the file zero byte
A simple hueristic for the above problem will be
If exists file then
If
wc -l dataflow gt threshold
cat >dev >null dataflow
Ftp servername (pick credentials from .netrc)
Put dataflow
Bye
Else
Ftp file intact.