July 2009
1 post
Better array search
Update: Added more data to show differences.
During the journey of finding a better linear array search function in javascript, I decided to write my own. I decided to take a different approach. Here’s the outcome.
Array.prototype.small_search = function(search) {
if (this[0] === search){ return 0; }
var s = '\x00';
var a = this.join(s);
var b = a.search(search)-1;
...
May 2009
9 posts
2 tags
How To: Splitting large text files into smaller...
Let’s say you have a huge text file with couple million lines. Now you want to split it up into 100, 200, or 300 line files. Bash makes it pretty easy with split.
In Bash:
split -l 500 file.txt
That splits up the file file.txt into 500 line chunks. What is produced are files such as xaa, xab, xac and so on.
Be sure to do man split for more options.
6 tags
How To: Find the size of a directory
Sometimes it’s very useful to know how much content is in a directory without opening a GUI interface.
In Bash:
du -cks * | sort -n | awk '\''BEGIN { split("KB,MB,GB,TB", Units, ","); } { u = 1;while ($1 >= 1024){$1 = $1 / 1024;u += 1;}$1 = sprintf("%.1f %s", $1, Units[u]);print $0;}'\'' | tail -11
I would suggest adding this to your bash aliases as ducks.
Output looks something...
3 tags
How To: Remove Duplicate Lines From A File
Sure you can load up a bulky editor and use its tools to do that, but why not do it in much faster in bash with awk.
In Bash:
awk '!x[$0]++' in.txt > out.txt
Wasn’t that easy?
9 tags
Command, Option, & Shift Symbols in Unicode
Unicode does define some other characters which are sort of Mac-specific.
⌘ - ⌘ - ⌘ - the Command Key symbol ⌥ - ⌥ - ⌥ - the Option Key symbol ⇧ - ⇧ - ⇧ - the Shift Key symbol ⎋ - ⎋ - ⎋ - the Power Button
Note: The Power Button and Shift Key are not Mac-specific. The power button is described as “broken...
2 tags
Keyboard Shortcut to Help Search Box
If you haven’t figured out how Mac OS X 10.5 (Leopard) help search box allows you to search menu items, check out Chris Pirillo demoing the feature.
The problem for me is that I’m a keyboard person. Over 90% of my day, I barely touch the mouse. Now, to get to the help search bar with your keyboard, just press:
⌘ (Command) + ⇧ (Shift) + ? (Question Mark)
3 tags
Find & Replace in Linux/Mac
This is a quick one liner to find and replace strings in *nix. You can use this in your Mac terminal or Unix prompt.
In Bash:
find ./ -type f -exec sed -i 's/FIND/REPLACE/g' {} \;
Example:
find ./ -type f -exec sed -i 's/signin/login/g' {} \;
6 tags
Firefox & Input Field Width
Hate the 2px difference you get when you apply a width on your input fields in Firefox?
Try this in CSS:
input, textarea, select{-moz-box-sizing: border-box;}
2 tags
See how many Kernel Panics your Mac has had
In bash:
ls -1 /Library/Logs/PanicReporter/ | wc -l
I’ve had 5.
1 tag
Scroll to bottom of page.
In javascript:
window.scroll(0, window.document.height);