We're going to learn a couple of useful commands for processing text. Before we begin, let's create a file to work with. Copy and paste the following command. After pasting, you will need to add a literal TAB character between "lazy" and "dog" (you can often do this by pressing Ctrl-v then TAB).
echo 'The quick brown; fox jumps over the lazy dog' > sample.txt
The first command we'll explore is cut, which extracts portions of text from a file.
Cutting by Character
You can extract content based on character position using the -c flag.
cut -c 5 sample.txt
This command outputs the 5th character from each line of the file. In our case, the output is "q". Note that spaces also count as characters.
Cutting by Field with cut f
A more powerful feature is cutting by fields. The cut f syntax, using the -f flag, allows you to extract text based on field position. By default, cut uses the TAB character as a delimiter, meaning everything separated by a TAB is considered a distinct field.
Let's see how to cut f based on fields:
cut -f 2 sample.txt
Since we inserted a TAB between "lazy" and "dog", this command treats "dog" as the second field. Your output should be "dog".
Using Custom Delimiters
You can also combine the field flag with the delimiter flag (-d) to specify a custom delimiter. This is useful when working with files that use characters like commas or semicolons to separate data.
cut -f 1 -d ";" sample.txt
This command changes the delimiter from a TAB to a semicolon (;). Since we are cutting the first field (-f 1), the result will be "The quick brown".