Unix How To: Sed & Awk -- Still friendly after all these years

By Sandra Henry-Stocker  6 comments

Even after decades of using Unix on thousands of systems, I find that it's still fun to discover various convolutions of sed and awk commands to perform command line wizardry. There's a lot more to each of these tools than those uses I make of these commands on a routine basis. Let's take a look at some one-liners you might not yet have tried.

One of my long-standing "tricks" for awk is using $NF to print the last field on every line. Since NF represents the number of fields on a line (e.g., 6), $NF represents the value of that last field (e.g., $6). Printing the last field of every line, therefore, might look like this:

$ awk '{print $NF}' myfile

or this:

$ awk -F: '{print $NF}' /etc/passwd

depending on whether the file is white space delimited or not.

You can get the length of every line in a text file by using awk's length command. For example, this command prints the length of every line in the /etc/motd file:

$ awk '{print length($0)}' /etc/motd

You can compute the square root of every fourth field in a file of numbers like so:

$ awk '{print sqrt($4)}' nums

You can display select lines from a chosen file.

$ awk '/nobody/ { print $0 }' /etc/passwd

You can generate an alphabetical listing of your users with an added sort command.

$ awk -F: '{ print $1 }' /etc/passwd | sort

You can count the lines in a file. In this example, we're using the END (only perform this after all lines have been processed) feature of awk to print the line number of the last line in the file.

$ awk 'END { print NR }' myfile

If you want to know how many words are in every line of a file, try this:

$ awk '{print NF}' myfile

You can add line numbers to every line in a file.

$ awk '{print NR, $0}' myfile

Sed, which I often find myself using only for those dead easy text substitutes such as sed "s/this/that/g" myfile also has some more interesting applications.

You can double space a file (appends a newline to every displayed line). Use 'G;G' (don't forget the quotes!) for triple spacing, 'G;G;G' for quadruple and so on.

$ sed G myfile

You can remove double spacing from a file.

$ sed 'n;d'

I've used sed to replace only the first instance of a string or every instance of a string in each line of text, but you can also use it to replace a specific instance -- for example, only the third or only the seventh instance.

$ sed 's/this/that/3'

You can also change multiple strings in a single sed command using semicolons like so:

$ sed 's/this/that/g;s/you/me/g;s/him/her/g' myfile

The sed and awk commands are far more versatile than I had remembered them being and could provide some useful aliases for you to toss into your startup files.

6 comments

    Anonymous 1 year ago
    People should use perl or thelike.
    Anonymous 1 year ago
    There are these great twitter and identi.ca accounts called @climagic that send out a few cool bash shell tricks and tips each day. You should check it out.
    Anonymous 1 year ago
    Thank you Sandra, another quality article. I use awk just about every day in my work. Much easier and quicker to develop a simple awk solution than trying to work out how to do the same in perl. I love awk so much I've found a port for dos/windows (it's a dos port) and use it exetensively in my windows environment. ( I have text base phone directory which I access using awk which is so much quicker than trying to find the number in our company intranet!).Sed and Awk still rule an the usurper Perl will just have to wait a little longer..... ;o)
    Anonymous 1 year ago
    Alternately, some of us who came to regular unix use later in the game get much the same effect out of "perl -n ...".
    Anonymous 1 year ago in reply to Anonymous
    I started learning UNIX (SunOS, AIX, HP-UX, Irix, and OSF/1) back in the early 1990s. At the time, I asked my senior team members which scripting language would serve me the best going forward. We were C/C++ programmers, but I also asked the STL admin (basically GOD at this famous government site).The unanimous response was perl, then sh. At the time, it wasn't safe to assume ksh was available on systems and vendors were in the process of migrating BSD-style programs to SYSV programs. Both were usually available, but sometimes not.That doesn't take away from awk, nawk, gawk, or whatever it is called now. I'm certain I have a few old scripts that use it. But perl included an awk-2-perl conversion script that worked well enough.Sed, OTOH, fills in nicely when writing bash scripts even today. sed rocks when perl is overkill.I still write perl almost daily. Many of the programs are non-trivial. I haven't written a C/C++ program in years.You know what awk needs to revitalize? OO-awk. Sorta like OO-Cobol, I guess.
    Anonymous 1 year ago
    I don't script everyday, but I have found the sed-oneliner and the awk-oneliner to be very handy tools. A quick google search will find them both and there is a Perl one-liner as well. Today, I found a bonus -- explanations for these cryptic oneliners. They are here: http://www.osnews.com/story/21004/Awk_and_Sed_One-Liners_Explained Keep up the good work Sandra...Enjoy...

      Add a comment

      Post a comment using one of these accounts
      Or join now
      At least 6 characters

      Note: Comment will appear soon after you have activated your account.
      Obscene/spam comments will be removed and accounts suspended.
      The information you submit is subject to our Privacy Policy and Terms of Service.

      ITworld LIVE

      Operating SystemsWhite Papers & Webcasts

      White Paper

      Microsoft Enterprise Agreement Program Overview

      Discover how flexible the Microsoft Enterprise Agreement Program is to help you build the right software solution agreement for your business. This paper highlights all the available options-from on-premise software and cloud service solutions, to payment options and enrollment programs, and more.

      White Paper

      Watson - A System Designed for Answers. The future of workload optimized systems design

      Watson is a workload optimized system designed for complex analytics, made possible by integrating massively parallel POWER7 processors and DeepQA technology. Read the white paper about Watson's workload optimized system design.

      White Paper

      Benefits of an ITIL Help Desk in the Cloud

      You can implement help desk processes based on ITIL easily with reduced up-front costs and automatic upgrades.  Used by more than 72,500 companies worldwide, the Force.com platform is the most widely used Cloud platform in the world.

      White Paper

      Top Considerations for Moving to a Cloud-based (SaaS) Delivery Model for I.T. Service Management

      Help Desk software as a service is attractive to many IT departments. It offers the same benefits of traditional IT help desk solutions, in addition to reducing capital expenses, accelerating implementation, and providing easier upgrades. This paper explores considerations for implementing IT service management in-house or as a service.

      White Paper

      Six Advantages of a Cloud-Based Help Desk

      Small and midsize companies shouldn't have to use less efficient, limited-functionality service desk solutions. Today's cloud-based help desks pack the same punch as on-premise enterprise solutions, without a large up-front investment or long installation time. Read more to find out how to get started with a cloud-based help desk today.

      See more White Papers | Webcasts

      Ask a question

      Ask a Question