I have some scripts which use awk to parse a CSV file. I have noticed that, if a cell is empty, awk simply moves to the next cell. This means, if I ask it to read column 4, but that cell is empty, it prints the data from column 5, e.g.:
echo "1@2@3@@5" | awk -F "@*" '{print $4}'
My expected result is that it will print nothing, because column 4 is empty.
- Why is
awkskipping column 4? - How can I get
awkto not ignore empty columns?
The problem is not what you think. awk is not ignoring empty cells; it is parsing that line as 4 fields instead of 5.
That’s becuase you’re using
@*as your field separator which allows one or more consecutive@as your field separator (@,@@,@@@, … are all valid field separators).Try using
-F "@"instead.