- Usage: Text substitution, filtering, and transformation.
- Syntax:
sed [options] 'command' file
- Options:
-i
: Edit files in-place.-e
: Multiple commands.-n
: Suppress automatic printing.
- Examples:
- Replace 'foo' with 'bar':
sed -i 's/foo/bar/g' file.txt
- Delete lines 2 to 5:
sed '2,5d' file.txt
- Print lines matching 'start' to 'end':
sed -n '/start/,/end/p' file.txt
- Append text after a line:
sed '/pattern/a new line of text' file.txt
- Replace 'foo' with 'bar':
- Usage: Processing and analyzing text files.
- Syntax:
awk [options] 'program' file
- Options:
-F
: Field separator.-v var=value
: Set variable.
- Examples:
- Print the second column:
awk '{print $2}' file.txt
- Sum and average of first column:
awk '{sum += $1} END {print "Sum:", sum, "Average:", sum/NR}' file.txt
- Print lines where third column > 5:
awk '$3 > 5' file.txt
- Use comma as separator:
awk -F, '{print $1}' file.csv
- Print the second column:
- Usage: Searching for text using patterns.
- Syntax:
grep [options] 'pattern' file
- Options:
-i
: Ignore case.-r
: Recursive search.-l
: List file names.
- Examples:
- Find 'pattern' in .txt files:
grep 'pattern' *.txt
- Case-insensitive search:
grep -i 'pattern' file.txt
- List files with 'pattern':
grep -l 'pattern' *
- Count 'pattern' occurrences:
grep -c 'pattern' file.txt
- Find 'pattern' in .txt files:
- Usage: Parsing and transforming JSON.
- Syntax:
jq 'filter' [file]
- Examples:
- Extract "name" key:
jq '.[] | .name' file.json
- Filter objects by age > 30:
jq '.[] | select(.age > 30)' file.json
- Increase quantity by 1:
jq '.items[] .quantity |= . + 1' file.json
- Combine fields into new object:
jq '{name: .name, total: .price * .quantity}' file.json
- Extract "name" key:
- Usage: YAML processing.
- Syntax:
yq [options] 'expression' [file]
- Options:
-i
: Update file in-place.-o=json
: Output as JSON.
- Examples:
- Extract value of key:
yq e '.key' file.yaml
- Update value:
yq e '.key = "newValue"' -i file.yaml
- Merge two YAML files:
yq ea '. as $item ireduce ({}; . * $item )' file1.yaml file2.yaml
- Convert YAML to JSON:
yq e -o=json file.yaml
- Extract value of key:
- Experiment safely: Test commands in a controlled environment.
- Chain Commands: Combine tools using pipes (
|
) for complex workflows. - Regular Expressions: Utilize regex for advanced pattern matching.