Peter Kazazes
Peter Kazazes

Reputation: 3628

Search and Replace with Numbers

Alright so I've got list in JS formatted like so:

{ text:"item1here", link: "0"},
{ text:"item2here", link: "0"},
...
{ text:"item2000here", link: "0"}

The list has over 2,000 items in it, I need a way to get the link: "0" to match that entries number in the list, starting with 0 and going upwards. Obviously some kind of search and replace would work find. Any ideas on how to go about this? I'm on a Mac so any UNIX or OS X method would be greatly appreciated.

Upvotes: 2

Views: 151

Answers (2)

Šime Vidas
Šime Vidas

Reputation: 186013

var i = 0;

for (var obj in names) {
    obj.link = String(i);
    i += 1;
}

(this is how you would do it programmatically in JavaScript)

Upvotes: 0

Jeff B
Jeff B

Reputation: 30099

This should do it:

 perl -e '$i=0; while(<>) { if(s/link\:\s+\"0\"/link: "$i"/){ $i++;} print; }' yourfilename > newfilename

There are lots more ways using various UNIX commands. Let me know if you need this explained in detail.

Upvotes: 2

Related Questions