Tiny Deathstars of Foulness

A number of commands available for finding positions that you want in a line and extracting only a certain amount of text can be pretty cumbersome in terms of learning curve. This isn’t to say that once you get the hang of them that they’re terribly complicated but it can take a little while to get the hang of them. And when you need something fast, you might want an easy command for extracting text from lines. In these cases, consider cut. The cut command doesn’t do regular expressions (I guess you could argue that its ability to use a delimiter can be used as a regular expression) and so it’s really easy to use. Basically, you feed cut some data and then tell it which characters in the line that you want to keep. It then gets rid of the rest. The easiest use of this is to look at a list of data. For example, let’s saw we have a file called test.txt with the following contents:
abc123 abc124 abc134 abc234 abd234 acd234
Now we’re going to cat the file (which just reads the file contents) and then pipe the output of reading that file into a cut command (which is done by simply adding a pipe character at the end of the first part of the command. Then we’re going to use the -c option of cut (which looks at character positions) to simply grab the first three positions (1-3) of the lines. The command would end up looking as follows:
cat test.txt | cut -c 1-3
And the output would look as follows (this output could then be redirected into a new file btw):
abc abc abc abc abd acd
You can also specify multiple ranges of characters (or single characters for that matter). For example, to see only characters 1-2 and 5-6:
cat test.txt | cut -c 1-2,5-6
Overall, cut is a very easy to use tool, with a limitation that your pattern that you are looking to maintain must be consistent in terms of the character position that you are using in each line. It also uses every line in a file; however, to go another step and look for all positions in a line only if the line has a pattern that it can match you could simply add a grep in the middle. For example, if you’re looking for each line of our sample text file that has the number 4 then you could do:
cat test.txt | grep 4
This would show you only the last five lines of the file since those are the only lines that have that number in them. You could then pipe the output of that file into your cut and, let’s say, look for characters 1-3 and 6 in the output:
cat test.txt | grep 4 | cut -c 1-3,6
Your result would then be the following:
abc4 abc4 abc4 abd4 acd4
Finally, there are going to be times when you’re not looking for a specific character position in a line but instead a character position or a pattern that begins with another pattern. For this you’re going to end up needing to use a more advanced tool, such as awk or if you’re feelin’ frisky (maybe I’m speaking for myself there) regex. These tools will have a steeper learning curve, but ultimately be far more useful.

January 17th, 2010

Posted In: Mac OS X, Mac OS X Server, Mac Security, Unix

Tags: , , , , , , , , ,

For some reason the uninstaller from Symantec doesn’t work in removing Norton (NAV 10). My guess, without delving into their uninstaller too deeply is that they ran into what I ran into, which is that the* processes are prefixed by a bracketed alphanumeric sequence. To get around this I listed them and used grep to grab each one, then awk to grab the label and did a launchctl stop against the label name once I had it. The rest of this script is pretty straight forward forcing the rm of each of the contents of the items from the snapshot plus the items from the pkg BoM. ┬áHere’s the script, or you can download it here:
#! /bin/bash launchctl stop `launchctl list | grep | awk ‘{print $3}’` launchctl stop `launchctl list | grep | awk ‘{print $3}’` launchctl stop `launchctl list | grep | awk ‘{print $3}’` launchctl stop `launchctl list | grep | awk ‘{print $3}’` kextunload -b com.Symantec.SymEvent.kext kextunload -b com.Symantec.SymOSXKernelUtilities.kext kextunload -b com.Symantec.kext.KTUM rm /etc/liveupdate.conf rm /etc/Symantec.conf rm /usr/bin/symsched rm /usr/bin/navx rm ~/Library/Preferences/com.Symantec.Scheduler.plist rm /Users/Shared/snorosx rm -rfd /Library/Contextual Menu Items/NAVCMPlugin.plugin rm -rfd /Applications/Symantec Solutions rm -rfd /Applications/Norton AntiVirus rm -rfd /Library/Receipts/NAVContextualMenu.pkg rm -rfd /Library/Receipts/NAVEngine.pkg rm -rfd /Library/Receipts/Norton AntiVirus.pkg rm -rfd /Library/Receipts/SymEvent.pkg rm -rfd /Library/Receipts/SymOSXKernelUtilities.pkg rm -rfd /Library/Receipts/NortonQuickMenu.pkg rm -rfd /Library/Receipts/SymSharedFrameworks.pkg rm -rfd /Library/Receipts/Norton AutoProtect.pkg rm -rfd /Library/Recepits/Symantec Scheduled Scans.pkg rm -rfd /Library/Recepits/Symantec Scheduled Scans.pkg rm -rfd /Library/Recepits/Symantec Scheduled Scans.pkg rm -rfd /Library/Receipts/navx.pkg rm -rfd /Library/Receipts/LiveUpdate.pkg rm -rfd /Library/Receipts/Symantec Scheduler.pkg rm -rfd /Library/Receipts/Stuffit.pkg rm -rfd /Library/Receipts/SymInstallExtras.pkg rm -rfd /Library/Receipts/SymHelpScripts.pkg rm -rfd /Library/Receipts/SymantecUninstaller.pkg rm -rfd /Library/Receipts/Symantec Alerts.pkg rm -rfd /Library/Application Support/Norton Solutions Support rm /Library/Application Support/NAV.history rm -rfd /Library/Application Support/Symantec rm -rfd /Library/PreferencePanes/SymantecQuickMenu.prefPane rm -rfd /Library/PreferencePanes/APPrefPane.prefPane rm -rfd /Library/PrivateFrameworks/SymAppKitAdditions.framework rm -rfd /Library/PrivateFrameworks/SymBase.framework rm -rfd /Library/PrivateFrameworks/SymNetworking.framework rm -rfd /Library/PrivateFrameworks/SymSystem.framework rm -rfd /Library/PrivateFrameworks/SymScheduler.framework rm -rfd /Library/StartupItems/NortonAutoProtect rm -rfd /Library/StartupItems/NortonMissedTasks rm -rfd /Library/Documentation/Help/Norton Help Scripts rm -rfd /Library/Widgets/Symantec Alerts.wdgt rm -rfd /System/Library/Extensions/SymEvent.kext rm -rfd /System/Library/Extensions/SymOSXKernelUtilities.kext rm -rfd /System/Library/Extensions/KTUM.kext rm /System/Library/Extensions.mkext.NxdE
Oh, since most everything I do on this site requires elevated privileges I usually forget to mention it, but this script will require those…

May 5th, 2009

Posted In: Mac OS X, Mac Security, Mass Deployment

Tags: , , , , ,

At a terminal prompt, it is really straight forward to grab the date, simply use the date command, with no arguments and you will get something similar to the following, including the day, date, time (with seconds), time zone and year:
Tue Apr 15 00:40:07 CDT 2009
In a script this can choose fairly challenging, especially in cases where you just need the date stamp without the time and time zone, etc. Here we’re going to grab the current system date from ESX, OS X or Linux (or whatever OSen really) and then use a variable, currentdate, to put that date, formatted into a pretty standard format, YYYYMMDD:
currentdate=`date ”+%c%m%d” | awk ‘{printf $5}’`
Which will output the date as follows:
Now, in our shell script we can create files, add lines to files, etc, with the shortened date stamp. Some of you will be using log analyzers that depend, for example, on unix epoch time. To grab the date formatted as such, the following command can be used:
date -j -f “%a %b %d %T %Z %Y” “`date`” “+%s”
The date command isn’t used as much to set time any more, since most systems rely on a Network Time Protocol (NTP) server to supply date and time information. However, it is worth noting that the date command can also be used to set the time on a computer.

April 15th, 2009

Posted In: Mac OS X, Mac OS X Server, Mass Deployment, Ubuntu, Unix, VMware

Tags: , ,

« Previous Page