So – sed and awk versions on the Mac are a little behind. Turns out if I write an expression to wrap a forward slash into braces that it doesn’t exactly translate when moving from Linux to a Mac. So this happened (make fun, it’s cool): #!/bin/bash #Extracts occurances of a file path from a text file #Then sorts and counts up each echo "Please make sure to run script from same Original_file PATH" read -p "Enter filename: " original_file cp $original_file tempfile.txt && sed 's/ /\n/g' tempfile.txt > tempfile1.txt && sed -i '.bak' '/^$/d' tempfile1.txt && cat tempfile1.txt | grep '^/' | sort |uniq -c | sort -k2nr |sort…
-
-
A Bit About Python Module Imports
The first lines of practically every python script ever begin with the word import. Per the python docs at https://docs.python.org/2/tutorial/modules.html a module is “a file containing Python definitions and statements.” To open a python environment, simply open Terminal and type Python python To import a module just type import followed by the name of the module. A common one is os, so let’s import that: import os Now let’s use str to see where it is: str(os) You’ll then see what the script was that is actually the os module and where it can be found/edited/viewed. Now let’s do a couple of basic tasks with os. First, let’s grab the…
-
Looping cURLy Braces
No, this article is not about my daughter’s curly hair and that expander thingie she had in her mouth for awhile (that sucked for her, booo). Instead we’re talking about expanding braces in shell scripts so we can expand braces in the script. What’s a brace? As with grammar, a brace is a type of bracket meant to denote a series of characters. The bash interpreter will then allow for the expansion to include a series, such as 1..100 or 1 to 100 or a..z or a through z, useful in loops where the variable name (num in the below example) when invoked in the loop as $num will expand…
-
Super-Simple Bash Graphs
The sparkr gem is installed by default in macOS. To use it to produce simple graphs, simply run it followed by a series of integers: sparkr 12 110 250 110 12 The result would be as follows: This is useful for a quick and dirty visualization in scripts. For example, a series of 5, 10, 200 numbers that don’t have that much range where you’re just looking for a simple pattern. Like number of lines in logs, etc. Obviously, you can pay a lot of money for graphing frameworks and very fancy-schmancy tools. This is really just for me in small scripts. Note: sparkr isn’t installed on all Mac systems.…
-
Use colrm to remove columns from a text file in bash
The colrm command is a simple little command that removes columns from standard input before displaying them on the screen (or piping the text into another file). To use, simply cat a file and then pipe it to colrm followed by the start and then stop in $1 and $2. For example, the following would only list the first column of a text file called testfile: cat testfile | colrm 2 Not providing a second column in the above command caused only the first column to be displayed to the screen. You could pipe all but the second and third columns of a file to another file called testfile2 using…
-
Install and Use Homebrew on macOS
Homebrew is a package manager for macOS. You can use Homebrew to install command line packages on a Mac, provided someone has written a formulae, which is a simple Ruby script that walks through the process for installing all the little bits required for a piece of software. Installing Homebrew is simple. Run the following command which is listed on the Homebrew homepage (not as root): /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" This will install the macOS Command Line Tools from Xcode as well as create the following directories (if they’re not already present): /usr/local/Cellar /usr/local/Homebrew /usr/local/Frameworks /usr/local/opt /usr/local/sbin /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var Then the script will move all the required bits…
-
Bash: if, then, else, and expressions
One of the most important skills in any language is to know how a basic if then statement works. You set a variable, you say if that variable matches a given pattern (or doesn’t) then do something or else do something different. If/then/else. Other than that, it’s just syntax, and the annoyance of figuring out the operators and syntax. So let’s go through the below script (which checks a Mac to see if grayscale is enabled in Universal Access), line-by-line: Line 1: we’re first going to create a variable called grayscale. We’ll fill it with a 0 or 1, the output of a simple “defaults read com.apple.universalaccess grayscale” command If that grayscale…
-
This New Years Day, Learn The Jot Command
This New Years Day, Learn The Jot Command The jot command is one I haven’t used in awhile. But it’s still useful. Let’s take a look at a few of the things you can do with it. First, let’s just print lines into a new file called “century.txt” which we can do by running with the number of increments followed by the starting number, and then redirecting the output into the file name: jot 100 1 > ~/Desktop/century.txt Or to do integers instead, simply put the decimals: jot 100 1.00 > ~/Desktop/century.txt Or in descending order, jot – 100 1 > ~/Desktop/century.txt Now we could change the output to be…
-
Quick and dirty: Pull a list of all filevault encrypted users on a Mac
In the following example script, I’m going to pull a list of just the usernames from fdesetup. sudo fdesetup list The output would be as follows: charlesedge,F4D8B61D-1234-1234-98F4-103470EE1234 emerald,2E1203EA-1234-4E0D-1234-717D27221234 admin,50058FCF-88DF-1234-1234-91FCF28C0488 I’ll then pipe them into sed and use the , as a delimiter, pulling * or everything before it: sudo fdesetup list | sed 's;,.*;;' As follows: charlesedge emerald admin
-
Is This Bash Command A Builtin?
Builtin commands are always kinda’ interesting. At first glance, it’s hard to know which commands are builtins. Luckily, there’s a command that I rarely use, called… command. If you run command with the -V flag it will tell you if the command is a builtin: command -V cd cd is a shell builtin If you run a command that isn’t a builtin command -V ls ls is /bin/ls Some builtins are in /bin (like echo). But not all builtins are in /bin. Some are in /usr/bin (like cd). Information about how to use builtins is built into the help command rather than standalone man pages. So, if you do help…