Skip to content
Advertisement

If a File is Empty, Write Text to First Line

I need to check to see if two files are empty. If they are, then write “-999” to the file, else run an average on the numbers within the file.

Here is the coding I’ve been trying to use:

Code:

JavaScript

Contents of file_all:

JavaScript

Advertisement

Answer

the appropriate way to check whether a file is empty is using -s not -z (which checks whether the given string is zero).

BTW, your code seems to calculate the median, while you state that you want to calculate the average, which is not the same. I’ve rewritten your script to calculate the average by using bash scripting and not awk.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement