Catalog / Scripting Utilities Cheatsheet
Scripting Utilities Cheatsheet
A comprehensive cheat sheet covering various scripting utilities, including `xargs`, `find`, `sed`, `awk`, `grep`, and `jq`. This cheat sheet provides a quick reference to essential commands, options, and examples to help you automate tasks and manipulate data efficiently.
xargs & find
xargs Basics
|
Build and execute command lines from standard input. Takes input from stdin and converts it to arguments for a command. |
|
General syntax. If |
|
Use at most |
|
Replace occurrences of |
|
Use at most |
|
Input items are terminated by the specified character. Useful when filenames contain spaces. |
find Basics
|
Search for files in a directory hierarchy. |
|
Base of file name (the path with the leading directories removed) matches shell pattern |
|
File is of type |
|
File’s data was last modified |
|
Execute |
|
Delete files; be careful when using this option. |
Combining xargs and find
Common use case: using
This command finds all |
sed
sed Basics
|
Apply |
|
Modify |
|
Substitute |
|
Apply |
|
Delete line. |
|
Print line. (Often used with |
sed Examples
Replace all occurrences of |
Replace all occurrences of |
Delete all lines starting with |
Print only lines that match |
Delete lines 2 through 5. |
Delete the last line. |
awk
awk Basics
|
Process |
|
Execute |
|
Execute |
|
The entire line. |
|
The first, second, etc. field (column) in the line. |
|
Number of fields in the current line. |
awk Examples
Print the first field of each line. |
Print the last field of each line. |
Print all lines that match |
Print all lines where the first field is greater than 10. |
Calculate the sum of the first field of all lines. |
Print the second field of each line in a CSV file, using |
grep & jq
grep Basics
|
Search for |
|
Case-insensitive search. |
|
Invert match. Select non-matching lines. |
|
Recursive search. |
|
Print line number with output lines. |
|
Print only a count of matching lines per file. |
grep Examples
Print all lines in |
Print all lines in |
Print all lines in |
Recursively search for |
Print all lines in |
Print the number of lines in |
jq Basics
|
JSON processor. If no file specified, reads from stdin. |
|
The identity filter. Outputs the input as is. |
|
Access the value associated with the key |
|
Access all elements in an array. |
|
Pipe filters. |
|
Output raw strings, not JSON. |
jq Examples
Pretty-print the JSON in |
Extract the value associated with the key |
Extract all elements from the |
Extract the |
Extract the age from each element of the top-level array. |
Fetch data from an API and extract the |