Command Line Basics: Cut Fields With awk

December 15, 2009 by
Filed under: command line, HowTo, linux, Ubuntu 

There are times when you will want to trim some information from the output of a command. This may be because you want to feed that output into another command. Whatever the reason for wanting to manipulate the output, awk is one of many tools available in GNU/Linux to perform this task.  

Now awk is a very powerful command and this demo is just a tiny fragment of what awk can do.

So let's say we want to know what kernels are installed in an Ubuntu system. The /boot directory has several files that reference the installed kernels in their file names. The command

ls /boot

will list these files. Notice that there are several files that reference the same kernel versions, so we'll just pick one set of these files and filter them by pipelining into the grep command. I'll use the pattern "vmlinuz".

Affiliate Link
ls /boot | grep vmlinuz

The output should look something like this:


Now I want to trim this output to remove the "vmlinuz-" portion from the front. I'll do that by pipelining into awk with "z-" as a field delimiter.

ls /boot | grep vmlinuz | awk -F'z-' '{ print $2 }'

The [-F'z-'] in the above command defines the field delimiter. The [print $2] statement that follows it tells awk to print the second field. My output looks like this.



One Response to “Command Line Basics: Cut Fields With awk”

  1. Story added...

    This story has been submitted to! If you think this story should be read by the free software community, come vote it up and discuss it here:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>