Replace spaces or -'s with _'s if line starts with a digit

Viewed 57

I have a text file as follows:

#1xx Informational responses
100=Continue
101=Switching Protocols
102=Processing
#2xx Success
200=OK
201=Created
202=Accepted
203=Non-Authoritative Information
204=No Content
205=Reset Content
206=Partial Content
207=Multi-Status
208=Already Reported
226=IM Used   

What I want to do is to convert this into the one below:

#1xx_Informational responses
100=Continue
101=Switching_Protocols
102=Processing
#2xx_Success
200=OK
201=Created
202=Accepted
203=Non_Authoritative_Information
204=No_Content
205=Reset_Content
206=Partial_Content
207=Multi_Status
208=Already_Reported
226=IM_Used

I could use:

:%s/[ \|-]/_/g  

but it unconditionally changes spaces or -'s with _'s in all lines, including the very first line as:

#1xx_Informational_responses

I do not want that. Instead, what I want to achieve is to replace spaces and -'s with _'s only if a line starts with a digit. For all other lines, I do not want to perform replace operation.

Is there any way to "conditionally" perform replace operation? I do not want to deal with line numbers, line numbers may change. I want answers with regarding the content of the line (starting with a digit for this case), not line number.

Thanks in advance.

2 Answers
Related