Skip to content

Latest commit

 

History

History
53 lines (34 loc) · 1.23 KB

awk.md

File metadata and controls

53 lines (34 loc) · 1.23 KB

Using awk

In many situations, I need some tools to make my life easier. On this page I describe awk or better some case I needed awk for.

What is awk

It's a scripting language for editing and analyzing texts. Input data is always processed line by line.

What a name

The name is derived from the initials of the developers

  • Alfred V. Aho
  • Peter J. Weinberger and
  • Brian W. Kernighan

Cases

Give is a string, separated by new lines with a tuple of values (e.g. ColumnX, ValueX, etc.).

No#
1 C1 C2 C3
2 V1 V2 V3
3 1 2 3
4 A B C

Print values of the 2. column

To print out the value of the second columns only:

echo "C1 C2 C3\nV1 V2 V3\n1 2 3\nA B C" | awk '{ print $2 }'

C2
V2
2
B

Changing the paramater $2 (e.g. to $1, $3) prints out the first or the third column.

Print values of the 2. column and 2. row

What I describe as a row named the oficial documentation record.

To print out a value of a specific record (e.g. the second), we can use NR (Number of Records) like this:

echo "C1 C2 C3\nV1 V2 V3\n1 2 3\nA B C" | awk 'NR==2 { print $2 }'

V2