Skip to content
Advertisement

Filling empty spaces in a CSV file

I have a CSV file where some columns are empty such as

JavaScript

How do I replace all the empty columns with the word “empty”. I have tried using awk(which is a command I am learning to use).

I want to have

JavaScript

I tried to replace just the 3rd column to see if I was on the right track

JavaScript

this left me with

JavaScript

I have also tried

JavaScript

this resulted in

JavaScript

Lastly I tried

JavaScript

this left me with

JavaScript

Advertisement

Answer

With a sed that supports EREs with a -E argument (e.g. GNU sed or OSX/BSD sed):

JavaScript

You need to do the substitution twice because given contiguous commas like ,,, one regexp match would use up the first 2 ,s and so you’d be left with ,empty,,.

The above would change a completely empty line into empty, let us know if that’s an issue.

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