Using cut
, multiple items can be retrieved with the same command. For instance, to return multiple columns from a line, there are two basic needs:
- Multiple columns that are not contiguous
- Multiple columns that are contiguous
The following commands split a string on dots, then return columns 1 and 3, then columns 1 through 3. The -d
flag specifies the delimiter and the -f
flag specifies the column(s) to return. Note that the delimiter is added back in to the results.
echo 100.200.300.400 |cut -d '.' -f 1,3
100.300
echo 100.200.300.400 |cut -d '.' -f 1-3
100.200.300
Read the man pages for concise documentation.