Subvert Normality

  • Archive
  • RSS

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!

  • 7 months ago
  • Permalink
  • Share
    Tweet
Pop-upView Separately
  • 1 year ago
  • Permalink
  • Share
    Tweet
Innocence
Pop-upView Separately

Innocence

  • 1 year ago
  • Permalink
  • Share
    Tweet

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.

  • 3 years ago
  • Permalink
  • Share
    Tweet
If you say “Curtoogle” three times in the mirror in a dark bathroom, Bill Gates appears with a broken Windows VPS
Rob in response to a horrible day dealing with Windows VPS and Severin alluding to using “curtgoogle” for an answer.
  • 3 years ago
  • Permalink
  • Share
    Tweet
You only get one life. Why not do something huge?
Paul Graham, Some Heros
  • 3 years ago
  • Permalink
  • Share
    Tweet
haha! :) Believe me, smart people are not abundant.
Eric
(A friend I was talking to about about some of the stupid business decisions hosting companies make.)
  • 3 years ago
  • Permalink
  • Share
    Tweet

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/

  • 3 years ago
  • Permalink
  • Share
    Tweet

I love it when Bruce Schneier comments on the absurd.

  • 3 years ago
  • Permalink
  • Share
    Tweet

Um, yeah… this frickin rocks.

  • 3 years ago
  • Permalink
  • Share
    Tweet
← Newer • Older →
Page 1 of 2

About

Twitter

loading tweets…

  • RSS
  • Random
  • Archive
  • Mobile

Effector Theme by Carlo Franco.

Powered by Tumblr