Mastering text manipulation with powerful tools
The command line has powerful tools for parsing and manipulating text.
Important note
Regular expressions, also known as regexes, are a sequence of characters that form a search pattern used to match, validate, and extract data from strings. It is a powerful tool used in programming, text processing, and data analysis to search, validate, and manipulate text. Regexes can be quite complex, but they are incredibly powerful. Many of the examples covered next can leverage regexes.
grep
grep pattern filename
searches for a pattern within a file. grep -r pattern directory
searches recursively within a directory. In this case, pattern
can be a regex, as described in the preceding note.
sed
sed 's/old/new/g' filename
replaces old
with new
in a file.
awk
awk '{print $1}' filename
prints the first column of a file. awk
splits the text in the file into columns based on white space...