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 9.6.2011 18.01, m.roth@5-cent.us wrote:
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.
Maybe just because I know vim better than sed, awk or perl, which I haven't used at all. :-)
The practical purpose is to turn a tabtext file into CSV (comma-separated) to be used in a SQL insert statement.
- Jussi
Jussi Hirvi wrote:
On 9.6.2011 18.01, m.roth@5-cent.us wrote:
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.
Maybe just because I know vim better than sed, awk or perl, which I haven't used at all. :-)
The practical purpose is to turn a tabtext file into CSV (comma-separated) to be used in a SQL insert statement.
Oh. I'm not that good on sed, but awk would be: awk '{gsub(/\t/, ",", $0);print $0;}' tabtextfile.
mark
On 6/9/2011 10:07 AM, Jussi Hirvi wrote:
On 9.6.2011 18.01, m.roth@5-cent.us wrote:
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.
Maybe just because I know vim better than sed, awk or perl, which I haven't used at all. :-)
The regexp parts will just have minor syntax differences.
The practical purpose is to turn a tabtext file into CSV (comma-separated) to be used in a SQL insert statement.
I'd highly recommend perl for this because it can also do the SQL part directly via DBI without all of the intermediate contortions you'll have to do in files otherwise. It should take about half a page of your own code to connect to the DB, read the file, transform it line-by-line to sql and execute the sql statements. And unlike other approaches with a pipeline of different tools, you can generate sensible error messages in the right places that have something to do with the input.
On 06/09/2011 08:37 AM, Les Mikesell wrote:
I'd highly recommend perl for this because it can also do the SQL part directly via DBI without all of the intermediate contortions you'll have to do in files otherwise. It should take about half a page of your own code to connect to the DB, read the file, transform it line-by-line to sql and execute the sql statements. And unlike other approaches with a pipeline of different tools, you can generate sensible error messages in the right places that have something to do with the input.
*AND* by using prepared statements in Perl you don't have to worry about escaping the text to prevent accidental SQL injections. It is all handled for you.
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@centos.org http://lists.centos.org/mailman/listinfo/centos
You 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
On Thu, Jun 9, 2011 at 11:03 AM, CS DBA cs_dba@consistentstate.com wrote:
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@centos.org http://lists.centos.org/mailman/listinfo/centos
You 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
Or you can have sed edit your file directly, just use the -i switch:
sed -e 's:find:replace:g' -i your.file.name
HTH, -at
From: Jussi Hirvi listmember@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