O'Reilly logo

Learning AWK Programming by Shiwang Kalkhanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Text conversion and substitution in files with AWK

In this section, we look at the working of one-liners in text conversion and substitution in files with AWK.

  • Let's create a sample file for practicing substitutions and conversions:
$ vi sample.txt    foo baz rubybaz foo foo foo        foo gold\silver foo silver foo baz foo foo foo            foo baz ruby
  • Convert Windows/DOS newlines (CRLF) to Unix newlines (LF) using AWK: In this example, we use the sub(regex, replacement, [string]) function. Here, we replace the \r (CR) character at the end of the line with nothing; we erase CR at the end. The print statement prints out the line and appends the ORS variable, which is \n by default. So, a line that ends with CRLF is converted into a line that ends with LF: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required