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
|
||||
|
||||
### Pattern with Action
|
||||
```awk
|
||||
# Pattern with Action
|
||||
pattern1 { action1 }
|
||||
```
|
||||
```awk
|
||||
pattern2 { action2 }
|
||||
```
|
||||
|
||||
## Variables
|
||||
|
||||
### Assigning Variables
|
||||
```awk
|
||||
# Assigning Variables
|
||||
variable = value
|
||||
|
||||
# Accessing Variables
|
||||
```
|
||||
### Accessing Variables
|
||||
```awk
|
||||
print variable
|
||||
```
|
||||
|
||||
## Operators
|
||||
|
||||
### Arithmetic Operators
|
||||
```awk
|
||||
# Arithmetic Operators
|
||||
+, -, *, /, %
|
||||
```
|
||||
|
||||
# Relational Operators
|
||||
### Relational Operators
|
||||
```awk
|
||||
==, !=, >, <, >=, <=
|
||||
```
|
||||
|
||||
# Logical Operators
|
||||
### Logical Operators
|
||||
```awk
|
||||
&&, ||
|
||||
```
|
||||
|
||||
## Built-in Variables
|
||||
|
||||
### NR: Record Number
|
||||
```awk
|
||||
# NR: Record Number
|
||||
NR > 1 {print "Line Number: " NR}
|
||||
|
||||
# NF: Number of Fields
|
||||
```
|
||||
### NF: Number of Fields
|
||||
```awk
|
||||
{print "Number of Fields: " NF}
|
||||
```
|
||||
|
||||
## Control Structures
|
||||
|
||||
### if Statement
|
||||
```awk
|
||||
# if Statement
|
||||
if (condition) {
|
||||
# Commands
|
||||
}
|
||||
|
||||
# if-else Statement
|
||||
```
|
||||
### if-else Statement
|
||||
```awk
|
||||
if (condition) {
|
||||
# Commands for true condition
|
||||
} else {
|
||||
|
|
@ -95,8 +103,8 @@ if (condition) {
|
|||
|
||||
## Functions
|
||||
|
||||
### Built-in Functions
|
||||
```awk
|
||||
# Built-in Functions
|
||||
{
|
||||
result = sqrt(9)
|
||||
print "Square Root: " result
|
||||
|
|
@ -106,14 +114,12 @@ if (condition) {
|
|||
## Text Processing
|
||||
|
||||
### Field Separator
|
||||
|
||||
```awk
|
||||
# Change Field Separator
|
||||
awk -F',' '{print $2}' filename
|
||||
```
|
||||
|
||||
### Output Formatting
|
||||
|
||||
```awk
|
||||
# Formatting Output
|
||||
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
|
||||
|
||||
### Match Pattern
|
||||
```awk
|
||||
# Match Pattern
|
||||
awk '/pattern/ {print}' filename
|
||||
```
|
||||
|
||||
## Arrays
|
||||
|
||||
### Declare an Array
|
||||
```awk
|
||||
# Declare an Array
|
||||
array[1] = "apple"
|
||||
array[2] = "banana"
|
||||
|
||||
# Access Array Element
|
||||
```
|
||||
### Access Array Element
|
||||
```awk
|
||||
print array[1]
|
||||
```
|
||||
|
||||
## User-Defined Functions
|
||||
|
||||
### Define a Function
|
||||
```awk
|
||||
# Define a Function
|
||||
function my_function(parameter) {
|
||||
# Commands
|
||||
}
|
||||
|
||||
# Call the Function
|
||||
```
|
||||
### Call the Function
|
||||
```awk
|
||||
my_function(value)
|
||||
```
|
||||
|
||||
## Command Execution
|
||||
|
||||
### Execute Command
|
||||
```awk
|
||||
# Execute Command
|
||||
awk '{cmd = "echo " $1; system(cmd)}' filename
|
||||
```
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue