Lesson 7.2: Transforming text: cut, tr, sed and awk
Four classic filters on a server inventory: cut takes columns out, tr translates or deletes characters, sed edits lines with substitutions (and can change files in place, with a backup), awk splits lines into fields and decides what to print.
20 min
You will learn to
- Cut fields out of delimited text with cut
- Translate, delete and squeeze characters with tr
- Substitute, print and delete lines with sed
- Edit a file in place with sed -i and keep a backup
- Print chosen fields and filter by a condition with awk
Key terms
- Field and delimiter
- Many text files are tables: each line is a record, split into fields by a delimiter such as a comma, a colon or a tab.
- cut
- Removes sections from each line: -d DELIM -f LIST for fields, -c LIST for character positions.
- tr
- Translates characters from SET1 to SET2, deletes them (-d) or squeezes repeats (-s). Reads only standard input.
- sed
- Stream editor: applies editing commands to each line. s/REGEX/TEXT/FLAGS substitutes, p prints, d deletes; an address (a number, a range or /pattern/) selects the lines.
- awk
- A pattern-action language: for each line, if the pattern matches, run the action in braces. Fields are $1, $2, ...; $0 is the whole line; NR is the line number; -F sets the field separator.
- diff
- Compares two files line by line: < lines exist only in the first, > only in the second.
← Searching text with grep · All lessons · Networking from the command line →