Showing posts with label Editors. Show all posts
Showing posts with label Editors. Show all posts

Monday, March 09, 2009

Find and replace a pattern using VIM

In recent past I am not using shell scripts and thus lost touch with powerful search replace options for patters.

As Windows Find replace options doesn’t support much of regex stuff, I resorted to VIM.

My intention to fix an XML file in structured format.

I have an xml file with elements like following (it is just a snippet of big file)

<PolicyNo>012345678</PolicyNo>
<DateOfCommencement>03DEC1983</DateOfCommencement>
<Plan_Term>579-60</Plan_Term>
<SumAssured>2,00,000</SumAssured>
<GrievanceRedressalOfficer>011-57293184</GrievanceRedressalOfficer>

And I wanted it in the format of

<DataPoint PolicyNo=”012345678”></DataPoint>
<DataPoint DateOfCommencement=”03DEC1983”></DataPoint>
<DataPoint Plan_Term=”579-60”</DataPoint>
<DataPoint SumAssured=”2,00,000”></DataPoint>
<DataPoint GrievanceRedressalOfficer=”011-57293184”</DataPoint>

Following expression did the trick. I got to tweak second back reference little a bit for different cases where non special characters are present.

And I could also prefix line numbers to confine operations to limited set of lines

like

:9,26s…..

:s/<\(\w\+\)>\(.\+\)<\/\(\w\+\)>/<Detail \1=”\2”><\/Detail>/gc

As usual this is a simple search replace syntax in the format of :s/<find string>/<replace string>/<options in this case gc>

Here find string is formatted as

  1. starting with ‘<’
  2. start of first back reference \(
  3. word of one of more length \w\+
  4. close of first back reference \)
  5. ending with ‘>’
  6. start of second back reference <\
  7. characters repeated till character ‘<’. I changed this for few lines
  8. escape for close charcter ‘/’
  9. closing tag. Though this is need not be back reference. I just did it
  10. ending with >

Now replace string is

  1. “<Detail “
  2. First back reference \1
  3. followed by ‘=’
  4. followed by opening quotes “
  5. second back reference ‘\2’
  6. followed by closing quotes ‘”’
  7. Closing tag syntax.

And the options include

g – all occurrences in a line

c – seek confirmation

Following links might be useful

VIM Regular Expressions

And my favorite regex site Regular Expressions Reference

Friday, July 27, 2007

Windows Command Prompt Customization

Now and then we happen to work on Windows Command Prompt. What makes me annoyed is its lengthy prompt. It becomes so irritating when you work deep inside hierarchy.

Here is an option to have path for ready reference as well as space to enter long commands.




To get this check for following registry settings

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor]
"AutoRun"="prompt $P$_$$"
"CompletionChar"=dword:00000040
"DefaultColor"=dword:00000000
"EnableExtensions"=dword:00000001
"PathCompletionChar"=dword:00000009


Here I added registry settings for path completion too.

Also I customized my title, you can see that in pic.

Thursday, April 20, 2006

Scitilla SciTE


I was using notepad++ on windows since long time, but I never bothered to look into what was behind it.

Just today I came to know about scintilla.org.
Downloded SciTE for linux with GTK+ support. I am on Gentoo 2006.0.First thought of looking for an ebuild, then relaised that is too much to expect.

Installation is damn simple. I didn't compile just tried binary version. Copied SciTE to /usr/local/bin and all properties files to /use/share/scite/

Experimented with cpp and py files. Ofcourse python integration would be netive thingi for this.
I happened to modify cpp.properties file to take ./ infront of executable file name.

Joined scintella intereset group. My first post to that group is
"bash like shell expects ./ before executable to run.cpp.propeties file line 283 need to be modified to like thiscommand.go.$(file.patterns.cplusplus)=./$(FileName)"

SciTE on windows.
This time I downloaded source and built using VC7 nmake.
Its quite simple just downloaded source of SciTE. SciTe source code includes Scitilla source also.

Built Scintilla library using nmake -f scintilla.mak from "scite168\scintilla\win32" directory,then scite use nmake -f scite.mak from scite168\scite\win32 directory.

Some hickups with setting the environment with vsvars32.bat in C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools directory. I was getting errors like nmake is not a recognised command.

Then I checked batch file, this file doesn't add anything to registry, it sets environemnt on current console.So the environement is valid for that console only.

I use cygwin on windows. SciTE can be used to build and execute. These options are present under Tools menu. Inorder to use gcc compiler, i added c:\cygwin\bin directory to path environment variable.

I just tested a simple cpp file. All works well. I am going to use SciTE till I find it inferior to anything else.

SciTE for windows

Today I tried SciTE for windows. This time I downloaded source and built using VC7 nmake.
Its quite simple just download source of SciTE. SciTe source code includes Scitilla source also.

First build Scintilla library using nmake -f scintilla.mak from "scite168\scintilla\win32" directory.
And to build scite use nmake -f scite.mak from scite168\scite\win32 directory.

Make sure that you set the environment with vsvars32.bat in C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools directory. This is important, otherwise you may get an error that nmake is not a recognised command. And this batch file doesn't add anything to registry, it sets environemnt on current console. So run this batch file and then call these nmake commands from same console.

Then I use cygwin on windows. SciTE can be used to build and execute. These options are present under Tools menu. Inorder to use gcc compiler, you may need to add c:\cygwin\bin directory to path. Ofcourse you need to have gcc installed in cygwin environment.

I just tested a simple cpp file. All works well. I am going to use SciTE till I find a better choice.

SciTE Scitilla.org

I was using notepad++ on windows since long time, but I never bothered to look into what was behind it.

Just today I came to know about scintilla.org.

Downloded SciTE for linux using GTK+. I am Gentoo 2006.0. First thought of looking for an ebuild, then relaised that is too much to expect.

Installation is damn simple. I didn't compile just tried binary version. Copied SciTE to /usr/local/bin and all properties files to /use/share/scite/

Experimented with cpp and py files. Ofcourse python integration would be netive thingi for this.

I happened to modify cpp.properties file to take ./ infront of executable file name.

Joined scintella intereset group.

My first post to that group is

"bash like shell expects ./ before executable to run.
cpp.propeties file line 283 need to be modified to like this
command.go.$(file.patterns.cplusplus)=./$(FileName)"

Let me see what comes back