awk - CSV file - field with single or multiple spaces

Hi,

In a csv file, I want to select records where first column has zero or multiple spaces.

Eg: abc.csv

,123,a
  ,22,b
    ,11,c
a,11,d

So output should be:

,123,a
  ,22,b
    ,11,c

Please advise

What have you tried so far?

Tried below options of awk.. but it works only for zero spaces.

 awk -F',' '! $1 ~  /./' 
awk -F',' '$1=="" {print $0}'
awk -F"," ' ! $1 ' file

Or

sed -n "/^ *,/p" file
1 Like

Hello vegasluxor,

Following may help you in same.

awk -F, '($1 == "" || $1 ~ /[[:space:]]/)'  Input_file

But if your first field has space and some value then that input will be catch also, as you didn't mention this condition in your input so I am giving you above solution. If you have first field only empty NOT a space with values following may help you in same too.

awk '{match($1,/^ *,/);A=substr($1,RSTART,RLENGTH);if(A){print $0}}'  Input_file

EDIT: Adding one more solution on same.

grep '^ *,'  Input_file

Thanks,
R. Singh

Thanks !!! :slight_smile:
second awk and grep worked!!!

---------- Post updated at 04:07 AM ---------- Previous update was at 04:01 AM ----------

@anbu
sed solution worked well!!! Thanks a lot :slight_smile: