Updated AWK sheet.
This commit is contained in:
parent
85037f19c3
commit
9c86a66a27
58
awk.md
58
awk.md
|
|
@ -38,54 +38,62 @@ awk '$1 > 10 {print}' filename
|
||||||
|
|
||||||
## Patterns and Actions
|
## Patterns and Actions
|
||||||
|
|
||||||
|
### Pattern with Action
|
||||||
```awk
|
```awk
|
||||||
# Pattern with Action
|
|
||||||
pattern1 { action1 }
|
pattern1 { action1 }
|
||||||
|
```
|
||||||
|
```awk
|
||||||
pattern2 { action2 }
|
pattern2 { action2 }
|
||||||
```
|
```
|
||||||
|
|
||||||
## Variables
|
## Variables
|
||||||
|
|
||||||
|
### Assigning Variables
|
||||||
```awk
|
```awk
|
||||||
# Assigning Variables
|
|
||||||
variable = value
|
variable = value
|
||||||
|
```
|
||||||
# Accessing Variables
|
### Accessing Variables
|
||||||
|
```awk
|
||||||
print variable
|
print variable
|
||||||
```
|
```
|
||||||
|
|
||||||
## Operators
|
## Operators
|
||||||
|
|
||||||
|
### Arithmetic Operators
|
||||||
```awk
|
```awk
|
||||||
# Arithmetic Operators
|
|
||||||
+, -, *, /, %
|
+, -, *, /, %
|
||||||
|
```
|
||||||
|
|
||||||
# Relational Operators
|
### Relational Operators
|
||||||
|
```awk
|
||||||
==, !=, >, <, >=, <=
|
==, !=, >, <, >=, <=
|
||||||
|
```
|
||||||
|
|
||||||
# Logical Operators
|
### Logical Operators
|
||||||
|
```awk
|
||||||
&&, ||
|
&&, ||
|
||||||
```
|
```
|
||||||
|
|
||||||
## Built-in Variables
|
## Built-in Variables
|
||||||
|
### NR: Record Number
|
||||||
```awk
|
```awk
|
||||||
# NR: Record Number
|
|
||||||
NR > 1 {print "Line Number: " NR}
|
NR > 1 {print "Line Number: " NR}
|
||||||
|
```
|
||||||
# NF: Number of Fields
|
### NF: Number of Fields
|
||||||
|
```awk
|
||||||
{print "Number of Fields: " NF}
|
{print "Number of Fields: " NF}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Control Structures
|
## Control Structures
|
||||||
|
|
||||||
|
### if Statement
|
||||||
```awk
|
```awk
|
||||||
# if Statement
|
|
||||||
if (condition) {
|
if (condition) {
|
||||||
# Commands
|
# Commands
|
||||||
}
|
}
|
||||||
|
```
|
||||||
# if-else Statement
|
### if-else Statement
|
||||||
|
```awk
|
||||||
if (condition) {
|
if (condition) {
|
||||||
# Commands for true condition
|
# Commands for true condition
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -95,8 +103,8 @@ if (condition) {
|
||||||
|
|
||||||
## Functions
|
## Functions
|
||||||
|
|
||||||
|
### Built-in Functions
|
||||||
```awk
|
```awk
|
||||||
# Built-in Functions
|
|
||||||
{
|
{
|
||||||
result = sqrt(9)
|
result = sqrt(9)
|
||||||
print "Square Root: " result
|
print "Square Root: " result
|
||||||
|
|
@ -106,14 +114,12 @@ if (condition) {
|
||||||
## Text Processing
|
## Text Processing
|
||||||
|
|
||||||
### Field Separator
|
### Field Separator
|
||||||
|
|
||||||
```awk
|
```awk
|
||||||
# Change Field Separator
|
# Change Field Separator
|
||||||
awk -F',' '{print $2}' filename
|
awk -F',' '{print $2}' filename
|
||||||
```
|
```
|
||||||
|
|
||||||
### Output Formatting
|
### Output Formatting
|
||||||
|
|
||||||
```awk
|
```awk
|
||||||
# Formatting Output
|
# Formatting Output
|
||||||
awk '{printf "Name: %-10s Age: %d\n", $1, $2}' filename
|
awk '{printf "Name: %-10s Age: %d\n", $1, $2}' filename
|
||||||
|
|
@ -121,38 +127,40 @@ awk '{printf "Name: %-10s Age: %d\n", $1, $2}' filename
|
||||||
|
|
||||||
## Regular Expressions
|
## Regular Expressions
|
||||||
|
|
||||||
|
### Match Pattern
|
||||||
```awk
|
```awk
|
||||||
# Match Pattern
|
|
||||||
awk '/pattern/ {print}' filename
|
awk '/pattern/ {print}' filename
|
||||||
```
|
```
|
||||||
|
|
||||||
## Arrays
|
## Arrays
|
||||||
|
|
||||||
|
### Declare an Array
|
||||||
```awk
|
```awk
|
||||||
# Declare an Array
|
|
||||||
array[1] = "apple"
|
array[1] = "apple"
|
||||||
array[2] = "banana"
|
array[2] = "banana"
|
||||||
|
```
|
||||||
# Access Array Element
|
### Access Array Element
|
||||||
|
```awk
|
||||||
print array[1]
|
print array[1]
|
||||||
```
|
```
|
||||||
|
|
||||||
## User-Defined Functions
|
## User-Defined Functions
|
||||||
|
|
||||||
|
### Define a Function
|
||||||
```awk
|
```awk
|
||||||
# Define a Function
|
|
||||||
function my_function(parameter) {
|
function my_function(parameter) {
|
||||||
# Commands
|
# Commands
|
||||||
}
|
}
|
||||||
|
```
|
||||||
# Call the Function
|
### Call the Function
|
||||||
|
```awk
|
||||||
my_function(value)
|
my_function(value)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Command Execution
|
## Command Execution
|
||||||
|
|
||||||
|
### Execute Command
|
||||||
```awk
|
```awk
|
||||||
# Execute Command
|
|
||||||
awk '{cmd = "echo " $1; system(cmd)}' filename
|
awk '{cmd = "echo " $1; system(cmd)}' filename
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue