change all files

73 views
Skip to first unread message

vicky b

unread,
Nov 6, 2012, 9:00:32 AM11/6/12
to v...@vim.org
HI I have list of files in a dir, each file has a key=value pair kind of entries so it basically a  prop file .I want to  change all they key=value in all  file to key,value is it possible using vim ,    sorry if it is off topi

--
Thanks & Regards
 Vickyb



Tim Chase

unread,
Nov 6, 2012, 9:18:18 AM11/6/12
to v...@vim.org, vicky b
On 11/06/12 08:00, vicky b wrote:
> HI I have list of files in a dir, each file has a key=value pair
> kind of entries so it basically a prop file .I want to change
> all they key=value in all file to key,value is it possible using
> vim , sorry if it is off topi


Depending on the complexity, it could be as simple as

vim *.txt
:argdo %s/=/,/e|w

or slightly less recklessly,

vim *.txt
:set hidden
:argdo %s/=/,/e
(survey the changes)
:xa " if they're good save & quit all, or
:qa! " if they're bad, abandon all changes

The :s command could be made more complex to tighten the
requirements, something like

%s/^\w\+\zs\s*=\s*\ze\</,/e

which ensures that there's a "word" at the beginning of the line,
followed by some optional whitespace (that will removed), the equals
sign (which will get changed to a comma) followed by more optional
whitespace (that will also get removed), followed by the start of a
word. This can be tweaked depending on the particulars of your
use-case.

-tim


vicky b

unread,
Nov 7, 2012, 8:07:05 AM11/7/12
to Tim Chase, v...@vim.org
Thanks tim that  did help 

I was able to do that but i have some unwanted lines in a file 

i just want line that have a pattern , below is the file 

#Generated by ResourceBundle Editor
# en Resource Bundle
#
# filename: FormsNRefsPortletResource_en.properties
# Portlet Info resource bundle example
Matches , \u0E15\u0E23\u0E07\u0E01\u0E31\u0E1A
Select , \u0E40\u0E25\u0E37\u0E2D\u0E01
choice1Label , \u0E40\u0E02\u0E49\u0E32\u0E23\u0E31\u0E1A\u0E01\u0E32\u0E23\u0E23\u0E31\u0E01\u0E29\u0E32


desired file should be like 

Matches , \u0E15\u0E23\u0E07\u0E01\u0E31\u0E1A
Select , \u0E40\u0E25\u0E37\u0E2D\u0E01
choice1Label , \u0E40\u0E02\u0E49\u0E32\u0E23\u0E31\u0E1A\u0E01\u0E32\u0E23\u0E23\u0E31\u0E01\u0E29\u0E32


that i want lines whic have \u0E15 these kind of stsring others i want to delete

Tim Chase

unread,
Nov 7, 2012, 8:56:29 AM11/7/12
to vicky b, v...@vim.org
On 11/07/12 07:07, vicky b wrote:
> I was able to do that but i have some unwanted lines in a file
>
> i just want line that have a pattern , below is the file
>
> #Generated by ResourceBundle Editor
> # en Resource Bundle
> #
> # filename: FormsNRefsPortletResource_en.properties
> # Portlet Info resource bundle example
> Matches , \u0E15\u0E23\u0E07\u0E01\u0E31\u0E1A
> Select , \u0E40\u0E25\u0E37\u0E2D\u0E01
> choice1Label ,
> \u0E40\u0E02\u0E49\u0E32\u0E23\u0E31\u0E1A\u0E01\u0E32\u0E23\u0E23\u0E31\u0E01\u0E29\u0E32
>
>
> desired file should be like
>
> Matches , \u0E15\u0E23\u0E07\u0E01\u0E31\u0E1A
> Select , \u0E40\u0E25\u0E37\u0E2D\u0E01
> choice1Label ,
> \u0E40\u0E02\u0E49\u0E32\u0E23\u0E31\u0E1A\u0E01\u0E32\u0E23\u0E23\u0E31\u0E01\u0E29\u0E32
>
>
> that i want lines whic have \u0E15 these kind of stsring others i want to
> delete

Well, there are a couple options, again using ":argdo" to iterate
over all the files. If you want to keep lines that contain
"backslash u hex-digit hex-digit hex-digit hex-digit", you can do

:set hidden
:argdo v/\\u\x\x\x\x/d

then review your changes and either save/abandon the changes.

Alternatively, if you have your original file and just want to keep
the "=" lines even if they don't have hex digits on them (might be
integers or something else?), you can preprocess with

:argdo v/=/d

to delete all the lines that don't contain an "=". You can then run
the previously-provided command to change the "=" to ",".

If you just want to delete those lines starting with a hash, you can use

:argdo g/^\s*#/d

to delete them.

-tim


vicky b

unread,
Nov 8, 2012, 6:53:51 AM11/8/12
to Tim Chase, v...@vim.org
THanks Tim dont get me wrong on this i have another query hope u wont mind,

  now i have 4 file where each file has key,value data , now i want to compare all the files get all the lines which have common keys in it , say 

 file1            file2                     file3           file
tim,chase    tom,someting    tom,wright   chase,w
tom,jerry      vinay,b              sachin,b      tom,m

out put would be
tom,chase
tom,jerry
tom,wright
tom,m


is this  possible in vim

Marcin Szamotulski

unread,
Nov 8, 2012, 8:10:48 AM11/8/12
to vim...@googlegroups.com
On 17:23 Thu 08 Nov , vicky b wrote:
> THanks Tim dont get me wrong on this i have another query hope u wont mind,
>
> now i have 4 file where each file has key,value data , now i want to
> compare all the files get all the lines which have common keys in it , say
>
> file1 file2 file3 file
> tim,chase tom,someting tom,wright chase,w
> tom,jerry vinay,b sachin,b tom,m
>
> out put would be
> tom,chase
> tom,jerry
> tom,wright
> tom,m
>
>
> is this possible in vim

Hi,

I guess you can use the :g command (":help :g"):

:argdo :g/^tom/#

:argdo - do the command for every file in the arg list
:g - run the # command (:help :#) for every line matching the pattern between /.../

If you want to put the output into another buffer you can do redirect it
to a register and paste it somewhere:

:redir @a
:argdo :g/^tom/
:redir end

Now you can paste the output using: "ap in normal mode.

If there is no command specified for the :g command it just prints the
line (:# prints the line with the line number).

Best,
Marcin

Tim Chase

unread,
Nov 8, 2012, 12:26:36 PM11/8/12
to vicky b, v...@vim.org
On 11/08/12 05:53, vicky b wrote:
> THanks Tim dont get me wrong on this i have another query hope u wont mind,

Nope...it's a pretty friendly list.

> now i have 4 file where each file has key,value data , now i want to
> compare all the files get all the lines which have common keys in it , say
>
> file1 file2 file3 file
> tim,chase tom,someting tom,wright chase,w
> tom,jerry vinay,b sachin,b tom,m
>
> out put would be
> tom,chase
> tom,jerry
> tom,wright
> tom,m
>
> is this possible in vim

I'm not sure I fully understand what you want here. I don't see any
obvious transformation from your 4 input files to the output you
request, so without a better understanding of what you want, it's
hard to produce a solution.

Operating across multiple files in vim is not always the easiest
thing to do, so I'd suggest a quick external script in
bash/perl/python/ruby.

-tim




John Little

unread,
Nov 9, 2012, 12:04:29 AM11/9/12
to vim...@googlegroups.com, Tim Chase, v...@vim.org
How about

1) Concatenate and sort all the files, either outside vim and load them into vim, or in, say

:for x in expand('file*',0,1) | exe 'r ' . x | endfor | sort

2) Decorate those of interest, say

:%s/^\(\(.\{-}\),.*\)\n\(\2,.*\)$/\1@!@\r\3@!@/

3) Delete the uninteresting:

:v/@!@/d

4) Remove the decorations:

:%s/@!@//

Steps 2,3,4 could be combined using a look behind regex, I imagine.

Regards, John

vicky b

unread,
Nov 14, 2012, 7:20:55 AM11/14/12
to vim...@googlegroups.com, Tim Chase, v...@vim.org
currently i am java developers who is addicted to eclipse( java ide), notepad++  
would it help me if i start learning vim consider it steep learning curve may not be now but would it help me in the long run 



--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

Ben Fritz

unread,
Nov 14, 2012, 9:37:41 AM11/14/12
to vim...@googlegroups.com, v...@vim.org
On Wednesday, November 14, 2012 6:21:25 AM UTC-6, vicky b wrote:
> currently i am java developers who is addicted to eclipse( java ide), notepad++  
> would it help me if i start learning vim consider it steep learning curve may not be now but would it help me in the long run 
>

Possibly; it depends on your editing style. My guess is "yes". Since you use both Eclipse and Notepad++, I'm guessing you like some of the developer tools in Eclipse but not the text editor.

Vim is not a very good IDE by itself but is an EXCELLENT text editor. And the eclim plugin for Eclipse and Vim lets you access many of the Eclipse developer tools you like from within Vim:

http://eclim.org

Next time please start a new thread rather than responding to an old one for a new topic like this.
Reply all
Reply to author
Forward
0 new messages