Re: VIM script replacement question
Nikos, The one line :%s way that was posted before is the truly elegant way of solving this problem. However, I found that macros are especially appropriate in this case. I was able to execute the whole task in 15 seconds by recording a macro for the first line, and then playing it back 26 times. Remember "q" is for recording macros. --Matt On Thu, May 25, 2006 at 05:26:16PM +0300, Nikolaos A. Patsopoulos wrote: > Hi all, > > I've got a series of files in the following format (tab delimited): > > 11000 > 20000 > 30000 > 41110 > 51100 > 60000 > 111101 > 121111 > 130000 > 140000 > 151000 > 161100 > 211101 > 221000 > 230100 > 241110 > 251111 > 260000 > 270000 > > > 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 > > Any ideas?? > > Thanks in advance, > > Nikos >
Re: VIM script replacement question
Alan G Isaac wrote: On Thu, 25 May 2006, "Nikolaos A. Patsopoulos" apparently wrote: 00 must become 0 0 The original replacement I sent had these spaces in it: :g/./s/^\(\d\+\)\s\+\([01]\)\s\+\([01]\)\s\+\([01]\)\s\+\([01]\)\s*/#\1\r\2 \3\r\4 \5 Look after \2 and after \4 hth, Alan Isaac Sth went wrong during copy&paste. Added the spaces manually and everything is ok. Thanks, Nikos
Re: VIM script replacement question
Tim Chase wrote: I get this: #3 00 00 #4 11 10 #5 11 00 How I can put spaces between numbers in same rows? Looks like you omitted spaces between the "\2" and "\3" and between the "\4" and "\5" in Alan's solution (or my 2nd one that broke out each piece individually) The final replacement should read /#\1\r\2 \3\r\4 \5/ ^ ^ with the two marked spaces. If you prefer tabs, you can change those spaces to "\t" or just type a character there. Or, if you already have the file, and there's only the two characters (0|1) on each line of interest, you can post-process it with :v/^#/s/./&\t :von every line that doesn't match ^#with a "#" at the beginnof the line ssubstitute .the first character you find &\twith that character followed by a tab (you can change the "\t" to a " ", but it doesn't show up quite as nicely in the email :) I'm not sure why ":%s" didn't work, but ":g/./s" did work for you...they should be effectively the same: with ":%s", if the match isn't found on the line (which is the case for lines that don't match "."), it skips the line. Peculiar. I suspect either an incomplete spec (the file wasn't what I copied&pasted from the original posting) or you have a funky mapping that was interfering (starting vim with "vim -u NONE" and then trying the examples we gave may solve matters). Or, alternatively, our one-line examples got copied over wrong or munged by mailers along the way. -tim Thanks Tim, it seems that spaces were omitted during copy and paste (!!). Don't no why :%s didn't work. I'll try later to find out why. Thanks again, Nikos
Re: VIM script replacement question
I get this: #3 00 00 #4 11 10 #5 11 00 How I can put spaces between numbers in same rows? Looks like you omitted spaces between the "\2" and "\3" and between the "\4" and "\5" in Alan's solution (or my 2nd one that broke out each piece individually) The final replacement should read /#\1\r\2 \3\r\4 \5/ ^ ^ with the two marked spaces. If you prefer tabs, you can change those spaces to "\t" or just type a character there. Or, if you already have the file, and there's only the two characters (0|1) on each line of interest, you can post-process it with :v/^#/s/./&\t :v on every line that doesn't match ^# with a "#" at the beginnof the line s substitute . the first character you find &\t with that character followed by a tab (you can change the "\t" to a " ", but it doesn't show up quite as nicely in the email :) I'm not sure why ":%s" didn't work, but ":g/./s" did work for you...they should be effectively the same: with ":%s", if the match isn't found on the line (which is the case for lines that don't match "."), it skips the line. Peculiar. I suspect either an incomplete spec (the file wasn't what I copied&pasted from the original posting) or you have a funky mapping that was interfering (starting vim with "vim -u NONE" and then trying the examples we gave may solve matters). Or, alternatively, our one-line examples got copied over wrong or munged by mailers along the way. -tim
Re: VIM script replacement question
Alan G Isaac wrote: On Thu, 25 May 2006, "Nikolaos A. Patsopoulos" apparently wrote: No, I used %. Got them same problem with Tim's code Something is not right ... Try using :g/./s/ instead of :%s/ and see what happens. hth, Alan Isaac One last question: I get this: #3 00 00 #4 11 10 #5 11 00 How I can put spaces between numbers in same rows? 00 must become>0 0 Don't want space after #3 though. Thanks, Nikos
Re: VIM script replacement question
Alan G Isaac wrote: On Thu, 25 May 2006, "Nikolaos A. Patsopoulos" apparently wrote: No, I used %. Got them same problem with Tim's code Something is not right ... Try using :g/./s/ instead of :%s/ and see what happens. hth, Alan Isaac Works great! Million thanks! Nikos
Re: VIM script replacement question
Alan G Isaac wrote: On Thu, 25 May 2006, "Nikolaos A. Patsopoulos" apparently wrote: The replacement didn't occur to the whole file. You must have forgotten the '%'. hth, Alan Isaac No, I used %. Got them same problem with Tim's code :(
Re: VIM script replacement question
Nikolaos A. Patsopoulos wrote: Hi all, I've got a series of files in the following format (tab delimited): 11000 20000 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
Re: VIM script replacement question
Alan G Isaac wrote: On Thu, 25 May 2006, "Nikolaos A. Patsopoulos" apparently wrote: 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\+\)\s\+\([01]\)\s\+\([01]\)\s\+\([01]\)\s\+\([01]\)\s*/#\1\r\2 \3\r\4 \5 hth, Alan Isaac Thanks for the answer,but sth is not working right. My output is this: 171100 180000 191111 201000 211101 221000 230100 #24 1 1 1 0 251111 260000 270000 The replacement didn't occur to the whole file. Thanks, Nikos
Re: VIM script replacement question
On Thu, 25 May 2006, "Nikolaos A. Patsopoulos" apparently wrote: > 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\+\)\s\+\([01]\)\s\+\([01]\)\s\+\([01]\)\s\+\([01]\)\s*/#\1\r\2 \3\r\4 \5 hth, Alan Isaac