Basic text processing with awk
Created by A. Martínez on 2019-05-10
4z.com
Table des matičres
Add a pattern to the elements of a column
Save the output of the processed file in another file
This document aims at explaining how to process a simple text file with "awk".
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
We will use the "awk" programming language as text processor.
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.
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).
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.
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.
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.
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.