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: ...