This cheatsheet provides common Bash, sed, and awk commands for text processing and troubleshooting, ideal for DevOps tasks and data manipulation in scripts.
Example: List files with detailed information.
ls -l
Example: Replace “foo” with “bar” in a file.
sed 's/foo/bar/g' file.txt
Example: Remove lines containing “delete”.
sed '/delete/d' file.txt
Example: Insert “New Line” before lines matching “pattern”.
sed '/pattern/i\New Line' file.txt
Example: Append “End of Line” after lines matching “pattern”.
sed '/pattern/a\End of Line' file.txt
Example: Replace digits with “#” symbol.
sed 's/[0-9]/#/g' file.txt
Example: Print first and third columns of a file separated by space.
awk '{print $1, $3}' file.txt
Example: Sum all numbers in the second column.
awk '{sum += $2} END {print sum}' file.txt
Example: Print lines where the third column is greater than 100.
awk '$3 > 100' file.txt
Example: Use comma as a field separator to print first column.
awk -F, '{print $1}' file.csv