Skip to content

Latest commit

 

History

History
53 lines (41 loc) · 1.7 KB

sed-awk.md

File metadata and controls

53 lines (41 loc) · 1.7 KB

sed, awk, grep, and the like - cheatsheet

sed and awk both read from stdin (or can be given a filename as an argument) and print to stdout.

sed

Recipies

Command Function
sed s/foo/bar/g file.txt Replace foo (regexp) with bar in file.txt.
sed 5 d Delete line 5.
sed /foo/d Delete lines matching foo.
sed -n 5,30 p Print lines 5 to 30.
sed -n s/foo/bar/p Print only changed lines.
sed i 17 foo Insert "foo" at line 17.
sed /foo/a bar Append "bar" after lines containing "foo".
sed s#foo#bar#g You don't have you use '/'s for regexes.
seg G Double space input.
sed -e "/12:00/,/12:10/!d" full.log > wanted.log Top 'n' tail a log file by time stamp.

Command line options

Option Effect
-n Supress normal output - use p to print
-i Replace file in place. BSD sed requires a suffix.

awk

Basic structure:

BEGIN {ACTION}
CONDITION {ACTION}
CONDITION {ACTION}
END {ACTION}
Command Function
awk -F, '{print $5}' Split on commas, print 5th token. $NF would give you the last.
awk 'BEGIN {s = 0} {s += $3} END {print s}' Sum 3rd column.
awk '/foo/ {print $0}' Print lines matching foo. $0 is the whole line.
awk '/foo/' As above - print is the default action.
awk 'length($0)' Pring long lines.
awk '/a/ && /b/ && !/c/ && /d/' Chained expressions - equivelent to `grep a

grep

Command Function
grep foo Include only lines condaining foo.
grep -v foo Exclide lines containing foo.
grep -o '{.*}' Include only part of lines matching pattern. (This example extracts the Json from a log file.