Awk is a useful language for many command line tasks. Often awk is used on the command line on its own or with strings piped to it, but it is possible to turn that awk code into an executable script. Consider the following script. This file contains awk code with a shebang of awk -f. The -f is …
Start reading Executable awk scripts in LinuxAuthor: Riyaz Cobra
In awk, fields are accessed by number; $1 for the first field, $2 for the second, etc. But sometimes the field number you want to access is not known until run time. In these cases, you can access the field using the $(<index>) syntax. The constant NF contains the number of fields available. For example, you can print all fields with their field …
Start reading Accessing fields by index in AwkEver wanted to have a real-time clock on your linux terminal? We can create one with a single line of bash, like so: Let’s look at how this works: date is a common unix tool used to print the current date and time. The $(…) means that the date command is run, and the output is placed here instead. For …
Start reading Linux Terminal ClockIf you have a filename or list of filenames, you may want to strip the extension. There are a few ways of “detecting” which part of the filename is the extension, but may not work if your file has multiple extensions (e.g. .tar.gz), contains spaces or periods, or meets other weird criteria. If you know …
Start reading Removing a known extension from a filenameIf you’ve got a string of items in bash which are delimited by a common character (comma, space, tab, etc) you can split that into an array quite easily. Simply (re)define the IFS variable to the delimiter character and assign the values to a new variable using the array=($<string_var>) syntax. The new variable will now be an array of …
Start reading Convert a delimited string into an array in BashSo you’ve got a USB drive, and you need to reformat it. You can do this all quite easily on the command line using the fdisk utility. First, get the device location. This will be something like /dev/sdd. You want the location of the device, not a partition on the device ie: /dev/sdd instead of /dev/sdd1. The instructions below work for …
Start reading Formatting a usb drive in linuxSometimes in a terminal you want to strip out the first line of output from a command. For example, you may want to generate a list of users which have tasks running using the ps command. This command puts a header at the top of the output. You can remove this header by piping the output to sed …
Start reading Ignoring the first line of output in a unix terminalEver jumped onto an Ubuntu server somewhere without knowing which operating system version it’s running? You can find this out with one simple command: This will provide output like:
Start reading Checking the Ubuntu version numberIn Bash you quite often need to check to see if a variable has been set or has a value other than an empty string. This can be done using the -n or -z string comparison operators. The -n operator checks whether the string is not null. Effectively, this will return true for every case except where the string contains no …
Start reading Checking for empty string in BashCVS is annoying in that if you want to find out which files have been modified or need updating, you can’t simply use the cvs status command as there’s too much information displayed. In order to make it useful, you really need to filter the output. Note: the following tutorial only works for linux computers using the …
Start reading List files which are not up to date in CVS