Basic text processing with awk

Created by A. Martínez on 2019-05-10

4z.com

Table des matičres

Basic text processing in bash. 1

Introduction. 1

Create a text file. 1

awk. 1

Add record number. 1

Change column order. 1

Add a pattern to the elements of a column. 1

Insert commas in the records. 1

Save the output of the processed file in another file. 1

Validation. 1

End of document. 1

 

Introduction

This document aims at explaining how to process a simple text file with "awk".

Create a text file

Let us create a test text file. This file will contain some records with name, surname, age, and color.

 

touch example.txt

ls -l

 

 

Once the file is created, we insert the records:

 

echo "John Maravilla 35 red" >> example.txt

echo "Alex Calt 22 green" >> example.txt

echo "Bruno Costa 40 white" >> example.txt

echo "William Lamarca 29 orange" >> example.txt

echo "Boris Ferguson 31 blue" >> example.txt

 

 

awk

We will use the "awk" programming language as text processor.

 

Add record number

                Sometimes, we can have a text file which has no record number. With "awk", it can be inserted easily:

 

awk '{print NR, $0}' example.txt

 

               

                The "NR" inserts the record number, and "$0" tells to "awk" to print the entire record.

 

Change column order

                Also, in some occasions we can find a text file with its columns disordered. We can reorder them:

 

awk '{print $1, $2, $4, $3}' example.txt

 

               

                "$1", "$2", "$4", and "$3" tell "awk" to print columns 1, 2, 4, and 3 (and in this order).

Add a pattern to the elements of a column

                Another frequent utilized capability of "awk" is adding prefixes or suffixes to the existing columns:            

 

awk '{print $1, $2, $3, $4"_color"}' example.txt

 

               

                The pattern to be added must be enclosed in quotation marks.

Insert commas in the records

We can transform the text file into a comma separate value file (csv file) with this command:

 

awk '{print $1 "," $2 "," $3 "," $4}' example.txt

 

               

                If the record had a big number of columns, we would have to design a more complex "awk" command.

 

Save the output of the processed file in another file

To save the output generated by "awk" is so simple. We have to just redirect the output to a file:

 

awk '{print $1 "," $2 "," $3 "," $4}' example.txt > example_processed.txt

 

Obviously, this can be applied to the rest of the cases described in the "awk" section.

Validation

Using the same "example.txt" text file, obtain the following output (all in just one command) and put it in another text file:

 

Add record number + change column order + add a pattern to the elements of a column + insert commas in the records**

 

**In this validation, you have to switch the 1st and 2nd column, and add the pattern "_years" to the years column.

 

Provide screenshots of every step.

End of document