Replace spaces with underscores up to first comma but not after the comma

I have a comma delimited file of major codes and descriptions. I want to replace all occurrences of spaces with underscores up to the first comma (only in the first field), but not replace spaces following the comma. For instance I have the following snippet of the file:

EK ED,Elementary and Kindergarten Education
OBGYN,Obstetrics/gynecology
BL EB,"Engineering, Business, and Human Development"
CONTE,Continuing Education Students
STAFF,Student Affairs
BANTH,Biological Anthropology
C PSY,Community Psychology
LAIDS,Liberal Arts Interdisciplinary Programs

The first line should become:

EK_ED,Elementary and Kindergarten Education

The second line should remain unchanged
The third line should become:

BL_EB,"Engineering, Business, and Human Development"

etc

Hello tdouty,

Welcome to forums, hope you will enjoy learning and sharing knowledge here.
Could you please try following.

awk -F, '{gsub(/[[:space:]]/,"_",$1)} 1'  Input_file

Output will be as follows.

EK_ED Elementary and Kindergarten Education
OBGYN,Obstetrics/gynecology
BL_EB "Engineering  Business  and Human Development"
CONTE,Continuing Education Students
STAFF,Student Affairs
BANTH,Biological Anthropology
C_PSY Community Psychology
LAIDS,Liberal Arts Interdisciplinary Programs
 

Thanks,
R. Singh

Awesome! That seemed to work. Thank you so much!

I used it this way:

cat all_major_codes.txt | awk -F, '{gsub(/[[:space:]]/,"_",$1)} 1' > new_file

To keep the comas (based on RavinderSingh13 post):

awk -F, '{gsub(/[[:space:]]/,"_",$1)} 1' OFS=, infile

Thank you. I did have to keep the commas so added that to the command.

One more thing (in case the line has no coma):

awk -F, '/,/ {gsub(/[[:space:]]/,"_",$1)} 1' OFS=, infile

The character class [:space:] may be sort of overkill if only spaces shall be replaced, as it consists of several chars, man isspace :

And, while we're at it, get rid of the unnecessary cat with:

awk -F, '{gsub(/[[:space:]]/,"_",$1)} 1' OFS=, all_major_codes.txt > new_file

if you want to change all whitespace characters to underscores, or:

awk -F, '{gsub(/ /,"_",$1)} 1' OFS=, all_major_codes.txt > new_file

if you just want to change <space> characters to underscores.