Srikanth
Srikanth

Reputation: 11

how to delete empty rows in delimited csv file in shell scripting

The empty line may contains other than comma also. for example:

file.csv

id,date,address
1,2-Oct,fhfhfhf
,,
"","",""
'','',''

result: file.csv

id,date,address
1,2-Oct,fhfhfhf

Thanks in Advance...

Upvotes: 0

Views: 1848

Answers (3)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Simple sed approach:

sed '/^[^-[:alnum:]]/d' file

The output:

id,date,address
1,2-Oct,fhfhfhf

Upvotes: 3

Ed Morton
Ed Morton

Reputation: 203532

$ grep "[^,'\"]" file
id,date,address
1,2-Oct,fhfhfhf

If that's not all you need then edit your question to include more truly representative sample input/output.

Upvotes: 1

Akshay Hegde
Akshay Hegde

Reputation: 16997

Input

$ cat file.csv 
id,date,address
1,2-Oct,fhfhfhf
,,
"","",""
'','',''

Output

$ awk -F, -v q="[\"']" 'FNR>1{ for(i=1; i<=NF; i++){ t=$i; gsub(q,"",t); if(t){print; next }} next }1' file.csv
id,date,address
1,2-Oct,fhfhfhf

Explanation

awk -F, -v q="[\"']" '                       # call awk set field separator and variable q
        FNR > 1 {                            # if no lines related to current file is greater than 1 then
                   for (i = 1; i <= NF; i++) # start loop through fields 
                   {
                     t = $i;                 # assign field contents to variable t
                     gsub(q, "", t);         # global substitution
                                             # suppress single/double quotes
                     if (t) {                # if still variable t has something
                                             # meaning row is not empty so
                       print;                # print current line
                       next                  # go to next line
                     }
                    }
                   next                      # all fields are empty 
                                             # so do not print, 
                                             # go to next line
               }1                            # does default operation print current line/row/record 
                                             # this is for printing header
                     ' file.csv

Upvotes: 0

Related Questions