Text Processing
Cut Fields
Selecting Columns from Text
cut extracts fields from delimited text. It is useful for small colon-separated, comma-separated, or tab-separated records.
Program
Play the script to select a field number from a colon-delimited record.
cut_fields.sh
#!/usr/bin/env bash
record="Ada:ops:active"
field=
value="$(printf '%s\n' "$record" | cut -d: -f "$field")"
echo "$field=$value"
#!/usr/bin/env bash
record="Ada:ops:active"
field=
value="$(printf '%s\n' "$record" | cut -d: -f "$field")"
echo "$field=$value"
#!/usr/bin/env bash
record="Ada:ops:active"
field=
value="$(printf '%s\n' "$record" | cut -d: -f "$field")"
echo "$field=$value"
delimiter
`-d:` tells `cut` that colon separates fields.
field number
`-f` chooses which delimited field to print.
text record
Simple delimited records are common in shell scripts and config files.