sed and awk: changing text and working with columns
Sign in to read this lesson
grep finds, sed changes, awk calculates with columns. In this lesson we learn to replace a value in a configuration file with one command (sed -i), to delete lines, and to extract and count the columns we need from a log (awk).
What you will learn
sed 's/old/new/'replaces the first match on each line,s/old/new/greplaces all of them.sed -ichanges the file itself; check without-ifirst, then keep a backup with-i.bak.sed '/pattern/d'deletes matching lines,sed -n '5,10p'shows only lines 5–10.awk '{print $1}'prints the first column;-F:changes the delimiter,NRis the line number,NFthe number of columns.- Conditions like
awk '$3 >= 1000'and calculations like{s+=$5} END {print s}are possible.
The lesson has 5 key points, an explanation and 5 questions. It opens with a package.