2022-01-16 18:20:39 +00:00
|
|
|
---
|
|
|
|
title: "awk"
|
2022-01-26 22:35:07 +00:00
|
|
|
tags: [ "Documentation", "System" ]
|
2022-01-16 18:20:39 +00:00
|
|
|
---
|
2020-01-02 00:04:35 +00:00
|
|
|
# Basics
|
|
|
|
|
|
|
|
See a file's contents:
|
|
|
|
|
|
|
|
Return full contents of a string:
|
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
|
|
|
awk '{ print }' file
|
|
|
|
```
|
2020-01-02 00:04:35 +00:00
|
|
|
|
|
|
|
Print the first and second column:
|
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
|
|
|
awk '{print$1$2}'
|
|
|
|
```
|
2020-01-02 00:04:35 +00:00
|
|
|
|
|
|
|
Return every line with the word 'the' (like grep):
|
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
|
|
|
awk '/the/{print}' file
|
|
|
|
```
|
2020-01-02 00:04:35 +00:00
|
|
|
|
|
|
|
Print everything containing a lowercase letter:
|
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
|
|
|
awk '/[a-z]/{print}' file
|
|
|
|
```
|
2020-01-02 00:04:35 +00:00
|
|
|
|
|
|
|
Same with numbers [0-9], or using a caret we can show lines starting with a number - ^[0-9], or ending with an uppercase letter - $[A-Z].
|
|
|
|
|
|
|
|
# Conditionals
|
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
|
|
|
awk '{ if($1 ~ /123/) print }' file
|
|
|
|
```
|
2020-01-02 00:04:35 +00:00
|
|
|
|
|
|
|
Check if the first column is equal to 1 or 2 or 3, and if so then print that line.
|
|
|
|
|
|
|
|
Grep for 'hawk' in a story:
|
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
|
|
|
awk '/hawk/' story.txt
|
|
|
|
```
|
2020-01-02 00:04:35 +00:00
|
|
|
|
|
|
|
Return any line with one or more "&" sequences:
|
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
|
|
|
awk '/&+/' script.sh
|
|
|
|
```
|
2020-01-02 00:04:35 +00:00
|
|
|
|
|
|
|
The pipe is used for 'or', so 'Orcs or drums' would be:
|
|
|
|
|
2023-06-17 19:28:20 +00:00
|
|
|
```bash
|
|
|
|
awk '/Orcs|Drums/' story.txt
|
|
|
|
```
|
2020-01-02 00:04:35 +00:00
|
|
|
|
|
|
|
Basic variables are:
|
|
|
|
|
|
|
|
- FS = Input field separator
|
|
|
|
|
|
|
|
- OFS = Output field separator
|
|
|
|
|
|
|
|
- NF = Number of fields on the current line
|
|
|
|
|
|
|
|
- NR = Number of records in the current file
|
|
|
|
|
|
|
|
- RS = Record separator
|
|
|
|
|
|
|
|
- ORS = Output record separator
|
|
|
|
|
|
|
|
- FILENAME = the file you're looking at.
|
|
|
|
|
|
|
|
So you can count the number of lines in a file, by referencing the number of 'end line' statements:
|
|
|
|
|
|
|
|
> awk 'END{print NR}' story.txt
|
|
|
|
|
|
|
|
Print line 2 from story.txt
|
|
|
|
|
|
|
|
> awk '{if(NR~/^2$/)print}' story.txt
|
|
|
|
|
|
|
|
Same, but any line starting with "2":
|
|
|
|
|
|
|
|
> awk '{if(NR~/^2/)print}' story.txt
|
|
|
|
|
|
|
|
|