Deletes the first character of each row and adds it using Vim

advertisements

I have a data file as follows.

1,14.23,1.71,2.43,15.6,127,2.8,3.06,.28,2.29,5.64,1.04,3.92,1065
1,13.2,1.78,2.14,11.2,100,2.65,2.76,.26,1.28,4.38,1.05,3.4,1050
1,13.16,2.36,2.67,18.6,101,2.8,3.24,.3,2.81,5.68,1.03,3.17,1185
1,14.37,1.95,2.5,16.8,113,3.85,3.49,.24,2.18,7.8,.86,3.45,1480
1,13.24,2.59,2.87,21,118,2.8,2.69,.39,1.82,4.32,1.04,2.93,735

Using vim, I want to reomve the 1's from each of the lines and append them to the end. The resultant file would look like this:

14.23,1.71,2.43,15.6,127,2.8,3.06,.28,2.29,5.64,1.04,3.92,1065,1
13.2,1.78,2.14,11.2,100,2.65,2.76,.26,1.28,4.38,1.05,3.4,1050,1
13.16,2.36,2.67,18.6,101,2.8,3.24,.3,2.81,5.68,1.03,3.17,1185,1
14.37,1.95,2.5,16.8,113,3.85,3.49,.24,2.18,7.8,.86,3.45,1480,1
13.24,2.59,2.87,21,118,2.8,2.69,.39,1.82,4.32,1.04,2.93,735,1

I was looking for an elegant way to do this.

Actually I tried it like

:%s/$/,/g

And then

:%s/$/^./g

But I could not make it to work.

EDIT : Well, actually I made one mistake in my question. In the data-file, the first character is not always 1, they are mixture of 1, 2 and 3. So, from all the answers from this questions, I came up with the solution --

:%s/^\([1-3]\),\(.*\)/\2,\1/g

and it is working now.


A regular expression that doesn't care which number, its digits, or separator you've used. That is, this would work for lines that have both 1 as their first number, or 114:

:%s/\([0-9]*\)\(.\)\(.*\)/\3\2\1/

Explanation:

:%s//           - Substitute every line (%)
\(<something>\) - Extract and store to \n
[0-9]*          - A number 0 or more times
.               - Every char, in this case,
.*              - Every char 0 or more times
\3\2\1          - Replace what is captured with \(\)

So: Cut up 1 , <the rest> to \1, \2 and \3 respectively, and reorder them.