miniguide

Bash, sed & awk Cheatsheet: Essential Command-Line Text Processing

Introduction

This cheatsheet provides common Bash, sed, and awk commands for text processing and troubleshooting, ideal for DevOps tasks and data manipulation in scripts.

Table of Contents

  1. Basic bash commands
  2. sed: Substitute text
  3. sed: Delete lines
  4. sed: Insert lines
  5. sed: Append text
  6. sed: Regex replacement
  7. awk: Print columns
  8. awk: Sum numerical data
  9. awk: Conditional printing
  10. awk: Field separator usage

1. Basic bash commands

Example: List files with detailed information.

ls -l

2. sed: Substitute text

Example: Replace “foo” with “bar” in a file.

sed 's/foo/bar/g' file.txt

3. sed: Delete lines

Example: Remove lines containing “delete”.

sed '/delete/d' file.txt

4. sed: Insert lines

Example: Insert “New Line” before lines matching “pattern”.

sed '/pattern/i\New Line' file.txt

5. sed: Append text

Example: Append “End of Line” after lines matching “pattern”.

sed '/pattern/a\End of Line' file.txt

6. sed: Regex replacement

Example: Replace digits with “#” symbol.

sed 's/[0-9]/#/g' file.txt

7. awk: Print columns

Example: Print first and third columns of a file separated by space.

awk '{print $1, $3}' file.txt

8. awk: Sum numerical data

Example: Sum all numbers in the second column.

awk '{sum += $2} END {print sum}' file.txt

9. awk: Conditional printing

Example: Print lines where the third column is greater than 100.

awk '$3 > 100' file.txt

10. awk: Field separator usage

Example: Use comma as a field separator to print first column.

awk -F, '{print $1}' file.csv