Nikolaos A. Patsopoulos wrote:
Hi all,

I've got a series of files in the following format (tab delimited):

1    1    0    0    0
2    0    0    0    0

I want to transform them in the following format:
#1
1 0
0 0
#2
0 0
0 0
etc..

In detail:
1.I want in front of the number in the first column to add "#" , then change line after the value
2. change line after 3rd column
3. change line after 5th column
4. repeat all three steps

:%s/^\(\d\+\)\t\(\d\+\t\d\+\)\t\(\d\+\t\d\+\)/#\1\r\2\r\3

should do the trick. It basically creates groups for each of the three things you're interested in (you can find them wrapped in "\(...\)" in the search regexp). The first thing you want is a bunch of digits ("\d\+", later referred to as "\1"). You want to ignore the tab ("\t"), then you want the "next bunch of digits followed by a tab, followed by the next bunch of digits" (later referred to as "\2"). You want to skip/ignore the next tab ("\t"). You then want the "next bunch of digits followed by a tab, followed by the next bunch of digits" (later referred to as "\3"). You then replace the whole mess with a hash-sign, the first number, a newline, the second pair of numbers, a newline, and the third pair of numbers.

If, as I found in the example you sent, there can be other white-space in there, you'll want to change the "\t" to "\s\+" meaning "at least some whitespace".

If you want to modify the whitespace between the 2nd/3rd and 4th/5th places, you can group them individually and then put the proper whitespace in the replacement:

:%s/^\(\d\+\)\s\+\(\d\+\)\s\+\(\d\+\)\s\+\(\d\+\)\s\+\(\d\+\)/#\1\r\2 \3\r\4 \5

Hope this both does the trick for you, and gives you some foundational explanation so if you need to tweak it in the future, you have some basis on which to do so.

-tim



Reply via email to