I am working on my first vim script. The script is supposed to do some find/replace on a file, then save the file with a new name and quit vim. I will save the script in a file and then call it from a bash script like this: vim path-to-the-file -s path-to-my-script Maybe I have not found the right resources. I can find/replace with expressions that are similar to those I use manually, for example: :% s/\t/","/g Then I should add something to the beginning of file (line 1, char 1). And append something to the end of the file (last line, last char). But I cannot find a way to do this. Should I move the cursor (and how?), or what? - Jussi
Jussi Hirvi wrote:> I am working on my first vim script. The script is supposed to do some > find/replace on a file, then save the file with a new name and quit vim. > > I will save the script in a file and then call it from a bash script > like this: > > vim path-to-the-file -s path-to-my-script > > Maybe I have not found the right resources. I can find/replace with > expressions that are similar to those I use manually, for example: > > :% s/\t/","/g > > Then I should add something to the beginning of file (line 1, char 1). > And append something to the end of the file (last line, last char). But > I cannot find a way to do this. Should I move the cursor (and how?), or > what?Why do vim scripting? That's what sed, or awk, or perl, are for. The latter two, of course, are much easier to comprehend the logic, too. mark
On 06/09/2011 08:48 AM, Jussi Hirvi wrote:> I am working on my first vim script. The script is supposed to do some > find/replace on a file, then save the file with a new name and quit vim. > > I will save the script in a file and then call it from a bash script > like this: > > vim path-to-the-file -s path-to-my-script > > Maybe I have not found the right resources. I can find/replace with > expressions that are similar to those I use manually, for example: > > :% s/\t/","/g > > Then I should add something to the beginning of file (line 1, char 1). > And append something to the end of the file (last line, last char). But > I cannot find a way to do this. Should I move the cursor (and how?), or > what? > > - Jussi > _______________________________________________ > CentOS mailing list > CentOS at centos.org > http://lists.centos.org/mailman/listinfo/centosYou can do this at the command line (or in a script) like this: sed "s/\t/","/g" [your file] > [new_modified_file] If needed then you can rename the modified file back over the original -- --------------------------------------------- Kevin Kempter - Constent State A PostgreSQL Professional Services Company www.consistentstate.com ---------------------------------------------
From: Jussi Hirvi <listmember at greenspot.fi>> ??? :% s/\t/","/g > Then I should add something to the beginning of file (line 1, char 1). > And append something to the end of the file (last line, last char). But > I cannot find a way to do this. Should I move the cursor (and how?), or > what?echo "First Line" > NEWFILE cat FILE | tr '\t' ',' >> NEWFILE? ? or?? sed 's/\t/,/g' FILE >> NEWFILE echo "Last Line" >> NEWFILE or awk ' BEGIN { print "First Line"; } { gsub(/\t/, ",", $0);print $0; } END { print "Last Line"; } ' FILE > NEWFILE JD
Jussi There is a good article on vimscript here: http://www.ibm.com/developerworks/linux/library/l-vimscript-1/index.html) Sent via BlackBerry by AT&T
Sorry there was a typo, the correct URL is: http://www.ibm.com/developerworks/linux/library/l-vim-script-1/index.html Sent via BlackBerry by AT&T -----Original Message----- From: flapeccino at gmail.com Date: Fri, 10 Jun 2011 15:39:12 To: CentOS mailing list<centos at centos.org> Reply-To: flapeccino at gmail.com Subject: Re: [CentOS] Vim scripting - cursor motion Jussi There is a good article on vimscript here: http://www.ibm.com/developerworks/linux/library/l-vimscript-1/index.html) Sent via BlackBerry by AT&T