$ command-helper

grep, sed, awk and friends

Searching text, replacing in files, extracting columns, sorting and counting.

Requires Google sign-in

grep

Search recursively with line numbers
grep -rn '<pattern>' <path>
Case-insensitive search
grep -i '<pattern>' <file>
Lines that do NOT match
grep -v '<pattern>' <file>
Show 3 lines of context around matches
grep -C 3 '<pattern>' <file>
Only file names with matches
grep -rl '<pattern>' <path>
Extended regex with several alternatives
grep -E 'error|warn|fatal' <file>
Fast search respecting .gitignore (ripgrep)
rg '<pattern>'

sed

Replace text in a file in place
sed -i 's/<old>/<new>/g' <file>

On macOS: sed -i '' 's/old/new/g' file

Print lines 10 to 20
sed -n '10,20p' <file>
Delete lines matching a pattern
sed -i '/<pattern>/d' <file>
Replace in all files of a projectadvanced
grep -rl '<old>' <path> | xargs sed -i 's/<old>/<new>/g'

awk

Print the first column
awk '{print $1}' <file>
Use a different separator (CSV)
awk -F',' '{print $2}' <file>
Sum a columnadvanced
awk '{sum += $3} END {print sum}' <file>
Rows where a column exceeds a valueadvanced
awk '$3 > 100' <file>

Sorting and counting

Count lines
wc -l <file>
Unique lines with counts, most frequent first
sort <file> | uniq -c | sort -rn | head
Top IPs in an nginx access logadvanced
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -20
Cut the 2nd field separated by colons
cut -d ':' -f 2 <file>
Compare two files
diff -u <file1> <file2>

Pipes and redirection

Write stdout and stderr to a file
<command> > output.log 2>&1
Show output and save it to a file
<command> | tee output.log
Run a command for each line of inputadvanced
cat <file> | xargs -I {} echo "item {}"