The cut command in Linux is used to extract sections from each line of a file or input data, typically by delimiter or character position. It’s useful for processing text files, logs, or command output.
Syntax:
cut [options] [file]
Common options:
-d: Specifies the delimiter (default is TAB).
-f: Selects the fields to be extracted.
-c: Selects specific character positions.
Examples:
1. Extracting specific fields (comma-delimited file):
cut -d’,’ -f1,3 file.txt
This extracts the first and third fields separated by commas.
2. Extracting characters by position:
echo “Hello World” | cut -c1-5
This extracts the first five characters: Hello.
3. Using cut with pipe:
ps aux | cut -d’ ‘ -f1,11
This extracts the username and command name from the ps aux output.
The cut command is effective for quickly extracting and processing data in text-based formats