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.
:g/^\d/s/[ -]/_/g
The :global command executes a command on each line matched by a given regex.
(By the way, your regex [ \|-] matches a character that is either a space or \ or | or -.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With