Archive
Posts in Shell
Using awk functions
I am a huge fan of awk, and find myself constantly using it to parse simple data steams. awk contains numerous string-related functions, which are an invaluable resource for awk script developers. To illustrate some of the cool awk functions, I created a single-line text file with the string "String of string": To get the length of each line in the file text, the length() function can be used: To see if the string "ing" is present in the file text, the index() function can be used: index() will return the location of the first occurrence of "ing," which can then be used to facilitate further string processing. To retrieve a range of characters in a string, a beginning and ending offset can be passed to the awk substr() function: And finally, to tokenize (split a line into word-length pieces) a string, the split() function can be used: I dig awk!
$ read more →Sectioning data with awk
I was working on a shell script last week and wanted to grab just the CPU section from the Solaris prtdiag(1m) output. I was able to perform this operation with awk by checking $0 for one or more "=" characters, and then setting a variable named SECTION to the value contained in the second position variable. If this variable was equal to the string CPUs, all subsequent lines would be printed up until the next block of "=" characters were detected. The awk script looked similar to the following: I really dig awk!
$ read more →Processing files with awk
I have used awk(1) for years to tokenize strings and to extract specific lines fom files. To tokenize a string, you can use awk(1)'s positional parameters when processing a file: When awk(1) processes the file /etc/services, each line will be split into tokens based on the value of IFS and placed into positional parameters (e.g., $1 ... $N). The awk(1) print function is then used to print all of the values passed as an argument…
$ read more →Is that an alias or a command?
When executing shell commands, I occasionally find that my alias definitions collide with actual commands. This periodically causes unexpected side effects, and munges the command output when additional options and pipelines are present. Since I use the Korn shell as my primary shell, I frequently use the whence(1) shell built-in to resolve these issues: whence(1) will provide a summary of the command passed as an argument, which can be helpful for determining if the command is an alias, function or an actual command. If you want to view all aliases, you can use the alias(1m) command without any options: UNIX shells contain lots of obscure commands and functionality, and are one of the coolest things to learn about!!
$ read more →Executing commands when the shell exits
While pruning some cruft from my .profile, I came across several important trap(1) statements that I had defined some time back. If you are unfamiliar with trap(1), this nifty little shell built-in allows the shell to setup signal handlers, which define a series of actions to take if a specific signal is received. The following trap statement will cause the script named '~/hi' to be executed when the user logs out of the shell: The integer argument to trap(1) indicates the SIGNAL to trap (these are defined in /usr/include/sys/iso/signal_iso.h on Solaris systems), and 0 is a trap(1) constant that will invoke the action when the shell exits.
$ read more →