The numerous faces of awk

By | August 13, 2021

If you happen to solely use awk when it is advisable choose particular fields from strains of textual content, you is likely to be lacking out on lots of different providers that the command can present. On this publish, we’ll take a look at this straightforward use together with many different issues that awk can do for you with sufficient examples to indicate you that the command is much more versatile than you may need imagined.

The best and mostly used service that awk gives is choosing particular fields from recordsdata or from knowledge that’s piped to it. With the default of utilizing white house as a subject separator, that is quite simple:

White house is any sequence of blanks and tabs. Within the instructions proven above, awk is extracting simply the fourth and first fields from the information offered.

Awk also can pull textual content from recordsdata by simply including the identify of the file after the awk command.

On this case, awk has picked out the primary, fifth, and final phrases within the single line of textual content.

 

 

The $NF specification within the command picks the final piece of textual content on every line. That’s as a result of NF represents the variety of fields in a line (23) whereas $NF represents the worth of that final subject (“coronary heart.”). The interval is included within the output as a result of it is a part of the ultimate string.

Fields might be printed in any order that you just would possibly discover helpful. On this instance, we’re rearranging the fields in date command output.

If you happen to omit the commas between the sector designators in an awk command, the output can be pushed right into a single string.

If you happen to substitute the same old commas with hyphens, awk will try and subtract one subject from another–probably not what you supposed. It does not take the hyphens as characters to be inserted into the print output. As an alternative, it places a few of its mathematical prowess into play.

On this case, it is subtracting 2 (the day of the month) from the yr (2021) and easily ignoring “Aug”.

If you need your output to be separated by one thing apart from white house, you’ll be able to specify your output separator with OFS (output subject separator) like this:

In case your date command output appears like what you see under, it is advisable change the fields to $7,$2,$3 within the instructions proven above for the reason that yr is listed final.

You can too use awk to easily show some textual content. After all, if all you need to do is print a line of textual content, you would be higher off utilizing an echo command. Then again, as a part of an awk script, printing some related textual content might be very helpful. This is a virtually ineffective instance:

This is a extra smart instance through which including a line of textual content to label your knowledge can assist determine what you are taking a look at:

Not all enter goes to be separated by white house. In case your textual content is separated by another character (e.g., commas, colons or semicolons), you’ll be able to inform awk through the use of the -F (enter separator) choice as proven right here:

This is a extra helpful instance – pulling a subject from the colon-separated /and so forth/passwd file:

You can too consider fields utilizing awk. If you happen to, for instance, need to record solely consumer accounts in /and so forth/passwd, you’ll be able to embrace a check for the third subject. Right here we’re solely going after UIDs which are 1000 and above:

If you wish to add a title in your itemizing, you’ll be able to add a BEGIN clause:

If you need multiple line in your title, you’ll be able to separate your supposed output strains with “n” (newline characters).

awk gives a stunning mathematical potential and might calculate sq. roots, logs, tangents, and so forth.

Listed below are a pair examples:

For extra particulars on awk‘s mathematical expertise, try Doing math with awk.

You can too write standalone scripts with awk. This is an instance that mimics one of many examples offered earlier, but additionally counts the variety of customers with accounts on the system.

Discover how the BEGIN part, which is run solely when the script begins, gives a heading, dictates the sector separator, and units up a counter to begin with 0. The script additionally consists of an END part which solely runs after all of the strains within the textual content offered to the script have been processed. It shows the ultimate depend of strains that meet the specification within the center part (third subject is 1,000 or bigger).

Use the script like this:

You should utilize awk to pick strains from a file that include a specified phrase or string. The examples under illustrate each with an allowance for a lowercase or uppercase “B” within the third.

To pick out a bit of textual content from a file, it is advisable embrace textual content from the beginning and finish strains that determine the portion to be extracted. This is an instance:

To alter textual content that is in a file utilizing awk, you should use syntax like that proven under. Simply remember the fact that this adjustments the textual content that you just see, however doesn’t modify the file contents. To save lots of the adjustments, redirect the output to a brief file after which use it to interchange the unique.

To switch a number of strings, use multiple gsub command:

To print the variety of strains in a file utilizing awk, do that:

The inclusion of END within the command means output is offered after strains have been processed. NR (variety of data) represents the variety of strains within the file.

To print the variety of phrases (or strings) on every line of a file, you should use a command like this:

You should utilize a script like that proven under to depend the phrases and supply simply the entire.

This script runs via the goal file a line at a time and provides the phrase depend for every line to the entire. This works as a result of NF represents the variety of fields in every line.

Alternately, you should use awk instructions like these to get the general and the per-line plus total counts:

You can too use awk together with numerous different instructions to view which instructions you might have used most steadily inside the life span of your present historical past file.

When you have added date and time fields to your historical past file, use $4 as a substitute of $2 within the command above.

An extended-standing Unix command, awk nonetheless gives very helpful providers and stays one of many causes that I fell in love with Unix many many years in the past. Whereas a few of awk‘s capabilities (like counting strains) might be extra simply carried out by different instructions (like wc and wc -l), it is nonetheless helpful to know what awk can do, particularly when you get into writing awk scripts and wish to make use of a lot of its capabilities.