Reputation: 7134
I want to replace ;
to ,
in a file .
Query ; Query Time ; Lock Time ; Rows Examined
Closed User Count ; 19.530083 ; 0.000090 ; 64;49;246
Expected Output:
Query ; Query Time ; Lock Time ; Rows Examined
Closed User Count ; 19.530083 ; 0.000090 ; 64,49,246
I use vim substitution to replace. But , I don't how to replace in this scenario.
Upvotes: 1
Views: 109
Reputation: 16190
:%s#\([^ ]\);\([^ ]\)#\1,\2#g
Explanation:
The %
aplies the command in every line.
Substitute command does not care which delimiter you use. It uses the first character after s
. When (a lot of) backslashes are used, I use e.g. #
.
It takes ("not-a-space" ; "not-a-space") and replaces it by (that 1st "not-a-space" , the 2nd "not-a-space")
g
at the end of the line makes it replace all the occurences per line (not just the first)
Upvotes: 1
Reputation: 496
Short answer:
:%s/(\d*);(\d*);(\d*)$/\1,\2,\3/gc
Explained:
%s/search/replace/gc
- will find all instances of search and swap them with replace. the c at the end makes it so vi will prompt you for each change
\(\d*\);\(\d*\);\(\d*\)
- as many numbers until you hit a semicolon, three times. the escaped parens will capture the output
\1,\2,\3
- escaped numbers give you the data we captured in the search string
Upvotes: 4
Reputation: 196496
A variant of Billy's answer that matches "one or more" digit instead of "0 or more":
:%s/\v(\d+);(\d+);(\d+)/\1,\2,\3
and another one that matches "at least 2" digits:
:%s/\v(\d{2,});(\d{2,});(\d{2,})/\1,\2,\3
Upvotes: 1
Reputation: 1715
Assuming all ";" are between digits:
:%s/\(\d\);\(\d\)/\1,\2/g
Or if you are looking for ";" not preceding by space
:%s/ \@<!;/,/g
Upvotes: 1