Skip navigation.

Redbeard's Thoughts

Everything you didn't want to know about what I'm thinking

Awk tip: ucfirst

, ,

How do you quickly convert the first letter of a word to upper case from the Linux (and other Unix-like) commandline without resorting to Perl? Try Awk.

Well, this is only my fourth post... and the first in almost a year. But I needed a place to put something, so here it is :smile:

I'm a programmer. And often I need to find a way to do things. Just now I needed to do an on text from the command line. I did a quick Google search and came up relatively empty. Someone gave an huge Awk script to do it, someone else suggested Perl. I wanted something simple, but I didn't want to use Perl. I thought Awk might be able to do it simply. And so it can.

The following code will uppercase the first letter of each word. I don't know how it handles multiple lines (I really only needed a single word, but this works for multiple space-separated words. It assumes the text to be converted is contained in the variable . In the discussion, the value of will be taken as "this is a test".

echo $text | awk 'BEGIN { RS=" "; FS=""; ORS=" "; OFS=""; } { $1=toupper($1); print $0 }'

Yep. That's it. For those of you not very familiar with Awk, let me break it down a bit. First, you need to know that Awk works with records (typically lines of text) and fields (typically space separated words). Then, apply the following.

  • The section gets run at the start of the process. In this case, I'm setting some variables.
  • The and variables set the Record Separator and Output Record Separator, respectively. Setting the variables causes Awk to use the new value to split and rejoin records. In this case we're setting the separator to be a space. In other words, each record is a single word.
  • The and variables set the Field Separator and Output Field Separator. Like the Record variables, these control the splitting and joining of fields. The empty string means we want each character to be a record.
  • The next block gets run for each record. In this case each word.
  • Inside a block, variables represented by a dollar sign ($) followed by a number represent a particular field. The variable represents the first field, the second, and so on. In this case, for the first record ("this"), takes the value of "t", takes "h", etc.
  • The variable represents the entire record.
  • The function converts then entire string to uppercase.

So the process, using the example text "this is a test", goes something like:

  1. Set Record and Field separators.
  2. Split the content into the records "this", "is", "a", and "test".
  3. Repeat the rest for each record.
  4. Convert the first letter to uppercase ("T", "I", "A", and "T").
  5. Output the entire record, using the modified first letter ("This", "Is", "A", "Test").

I hope this helps someone somewhere.

HTTP Panel for OperaWelcome - Again

Write a comment

You must be logged in to write a comment. If you're not a registered member, please sign up.

Download Opera, the fastest and most secure browser
January 2010
S M T W T F S
December 2009February 2010
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30