Thirty Minute Solution with Vim
Today on IRC a friend had an issue he was trying to solve using Vim. He had a sed/awk solution but was interested in seeing if it could be solved in Vim.
The problem was a common one, one that I’ve run into before. I’ve got this giant list of data but all of the primary key’s are ‘1’. How do I automate the process of incrementing the primary key?
Here’s a sample of the data:
[
{
"pk": 1,
"model": "app.model",
"fields": {
..
}
},
{
"pk": 1,
"model": "app.model",
"fields": {
..
}
},
{
"pk": 1,
"model": "app.model",
"fields": {
..
}
},
...
]
You can see there that all the “pk” lines contain a ‘1’.
So how do we do this in Vim?
First we looked at how to match the “pk” on each line, which can be done by this search parameter in Vim:
:g/pk/
Then we needed to get to the digit, so we added:
:g/pk/s/\d\+
The \d+ finds the digits on that line (the ‘+’ just matches more than one digit). We are putting this in a s/ since we eventually want to search and replace the ‘1’ with an incremented number.
So far we are on the proper line, we’ve matched the digits on that line, how do we increment them?
We need to start at one (at the top of the file) so lets add to our line above:
:let i=1|1,$g/pk/s/\d\+/
Here we’ve set the variable “i” to 1. We’ve also stated in “1,$” that we want to match the entire file. This can actually be shortened to “%”.
Now we need to change \d+ to “i”, and then increment “i”.
:let i=1|%g/pk/s/\d\+/\=i/g|let i=i+1
Huh? Lets take a look at what we’ve added.
- We shortened our ‘whole’ file setup to ‘%’
- We’ve added the replacement value ‘\=i’
- We want to match the entire file ‘/g’
- And finally we increment “i” with ‘let i=i+1’
Lets run that on our example data and see what we get?
[
{
"pk": 1,
"model": "app.model",
"fields": {
..
}
},
{
"pk": 2,
"model": "app.model",
"fields": {
..
}
},
{
"pk": 3,
"model": "app.model",
"fields": {
..
}
},
...
]
Bingo!
Innocence
The Lord’s Prayer
I found this today and thought it was worth repeating just the translated version. Being a computer geek allows me to enjoy such things. :)
Our sysadmin, who chills in Heaven, feared be thy name. Thy pwnage come, thy scripts be done in /earth as it is in /heaven. Seed us this day our daily ROMs. Forgive us our n00b exploits as we forgive script kiddies that hax0r against us. Reveal to us not 0 day vulns, but save us from RIAA. For thine is the network, and the rm -rf /, and all our base are belong to you, forever and ever. Amen.
If you say “Curtoogle” three times in the mirror in a dark bathroom, Bill Gates appears with a broken Windows VPS
You only get one life. Why not do something huge?
haha! :) Believe me, smart people are not abundant.
(A friend I was talking to about about some of the stupid business decisions hosting companies make.)
Interesting…
1. Focus on technique as opposed to outcome.
2. Set specific goals.
3. Get good, prompt feedback, and use it.
http://freakonomics.blogs.nytimes.com/2008/03/11/how-did-a-rod-get-so-good/
Um, yeah… this frickin rocks.