I have a .csv file that contains 3 columns like this:


Is there any way to check whether the second line of this .csv file is empty? Something like this:

if second-line-empty; then
   echo "empty"
   echo "not empty"

In this case, all three fields in the 2nd line should be empty and my file should only contain this line:

  • 1
    So you want exactly one line with Name,Last,Value? You talk about the second line being empty, what if it's empty and there's a third (or more) lines that are not empty? Do you know for sure that there will always be at least one line, and that it will contain the header fields? – Andy Dalton Apr 9 at 13:20
  • Would it be enough to detect a single line? – Kusalananda Apr 9 at 13:22
  • @AndyDalton Yes in my intended script, if the second line is empty, it means all the following lines are empty as well, So I just want to know if the 2nd line is empty – Pablo Apr 9 at 13:22
  • @Kusalananda Yes, if my .csv file contains only the first line, I want to be informed. – Pablo Apr 9 at 13:23
if [ "$(wc -l <file.csv)" -eq 1 ]; then
    echo file only contains a single line

This runs wc -l on the file's contents. If this results in a count of 1 line, then the file only contains a single line.

Note that an "empty line" contains a newline character (but nothing else). The code above will detect whether the file does not contain a second line at all, as in the example that you show at the end of the question.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.