2. cut
-
The command
cut
extracts a certain column (field) from the input, for example:cut -f 3 distros.txt
cut -f 1,3 distros.txt
cut -f 1-2,3 distros.txt
-
If we want to extract only the year, we can do it like this:
cut -f 3 distros.txt | cut -c 7-10
The option
-c
tellcut
to extract from the line characters, instead of fields (as if each character is a field).cut -f 3 distros.txt | cut -c 7-10,1-2,4-5
-
Another way to get the year would be like this:
expand distros.txt | cut -c 23-
The command
expand
replaces tabs by the corresponding number of spaces, so that the year would always start at the position 23. -
When working with fields, it is possible to specify a different field delimiter, instead of the tab. For example:
head /etc/passwd
cut -d ':' -f 1 /etc/passwd | head
Here we extract the first field from
/etc/passwd
.
Download lesson07/part2.cast
Loading asciinema cast...