# cut

> Source: Courseiva IT Certification Glossary — https://courseiva.com/glossary/cut

## Quick definition

The cut command lets you pick out specific pieces of information from lines of text. You tell it what separator to use, like a comma or space, and which columns you want. It then prints only those parts, making it easy to extract data from logs, CSV files, or output from other commands.

## Simple meaning

Imagine you have a long list of names and phone numbers, with each entry separated by a comma. You only need the phone numbers, not the names. Instead of going through each line manually and copying the number, you could use a tool that says 'take everything after the comma and show me just that.' That is what the cut command does, but for text on a computer.

Think of cut like a pair of scissors that snips out specific parts of a sentence. If a line of text is like a train with multiple cars (fields), cut lets you pick which cars to unhook and display. You can decide the point where the train should be cut, usually by a delimiter like a comma, space, or tab. Then you specify which cars (fields) you want to see, like the first car or the second and fourth cars.

In everyday life, you might use cut when you have a spreadsheet saved as a CSV file. Each row is a line, and each cell is a field separated by a comma. If you only need the email addresses from a list of names, emails, and phone numbers, cut can extract just the email column for you. It is a fast, no-frills tool that works directly in your terminal, and it is especially useful when combined with other commands like grep or sort in a pipeline. You do not need a full programming language to do simple text extraction; cut does one thing and does it well.

## Technical definition

The cut command is a standard Unix/POSIX utility used to extract sections from each line of input, typically from a file or standard input. It operates by splitting each line into fields based on a delimiter or by selecting specific bytes or characters. The core functionality is defined in the POSIX.1-2008 standard, and it is implemented in virtually all Unix-like systems, including Linux, macOS, and Windows via WSL or Cygwin.

Cut uses three primary selection modes: byte positions (-b), character positions (-c), and delimited fields (-f). The byte mode extracts specific bytes from each line, useful for fixed-width records. The character mode works similarly but accounts for multi-byte characters depending on the locale. The field mode is the most common in scripting and automation; it splits lines using a delimiter specified with the -d option (default tab) and outputs the chosen fields.

For example, cut -d',' -f2,4 input.csv extracts the second and fourth comma-separated fields from each line. The delimiter is a single character, and fields are numbered starting from 1. Ranges are supported, such as -f1-3 for fields one through three, or -f2- for all fields from the second onward. The --output-delimiter option allows setting a custom separator for output, which is useful when transforming data.

In real IT implementation, cut is frequently used in shell scripts and automation pipelines to process log files, configuration files, or command output. For instance, parsing the /etc/passwd file uses cut -d':' -f1,6 to extract usernames and home directories. It pairs well with grep to filter lines, then cut to extract specific columns. In DevOps, cut can isolate IP addresses from access logs or parse environment variables.

Limitations exist: cut cannot handle variable-length whitespace delimiters without preprocessing (e.g., using tr -s to squeeze spaces), and it does not support multi-character delimiters directly. However, alternative tools like awk or perl offer more advanced parsing. For exam contexts, understanding cut's behavior with delimiters, especially the delimiter option and field numbering, is critical. The command is simple but powerful when combined with other text processing commands in a pipeline.

## Real-life example

Imagine you are a teacher with a class roster saved as a text file. Each line contains a student's full name, their grade, and their email address, all separated by commas. You need to send an email to every student, so you only need the email addresses. You do not want to copy each one by hand, that would take too long and risk mistakes.

You open your terminal and use cut -d',' -f3 roster.txt. This tells the computer: 'Look at the roster file. For every line, find the commas. Then give me the third item after each comma.' In seconds, you have a clean list of just email addresses, ready to paste into your email client.

Now, consider a similar situation but with a twist. Your file uses a semicolon instead of a comma, like 'John Doe;85;john@school.edu'. You simply change the delimiter: cut -d';' -f3 roster.txt. The command works exactly the same way, just with a different separator. This flexibility is why cut is so useful in real-world scripting, you can adapt it to any standard text format.

In IT, this is exactly how administrators extract data from system logs or configuration files. For example, you might have a log file where each entry is '2025-03-15 10:30:45 ERROR: disk full /dev/sda1'. Using cut with space as delimiter, you could extract just the timestamp or just the error message. The ability to quickly slice and reshape text is a fundamental skill in scripting and automation.

## Why it matters

In IT, you are constantly dealing with text-based data. Log files, configuration files, CSV exports, command outputs, all come as lines of text. The ability to extract exactly the information you need from these streams is a core skill for scripting and automation. Without tools like cut, you would have to manually parse text or write more complex code, slowing down troubleshooting and automation tasks.

For system administrators, cut is invaluable for quickly parsing system files. For example, extracting a list of users from /etc/passwd, pulling IP addresses from access logs, or isolating error codes from application logs. In DevOps and CI/CD pipelines, cut is often used in shell scripts to extract version numbers, timestamps, or build artifacts from command output.

Cut is also a building block for more complex text processing. When you chain cut with grep, sort, uniq, and awk, you create powerful data extraction and reporting tools. It is lightweight, fast, and available on every Unix-like system, making it a reliable tool for ad-hoc analysis and automated scripts.

understanding cut prepares you for concepts like field parsing, delimiters, and text streams, foundational knowledge that carries over to programming languages and database querying. For certification exams, cut appears in questions about shell scripting, text processing, and Linux command proficiency. Mastering it demonstrates a practical understanding of command-line efficiency.

## Why it matters in exams

The cut command is a frequent topic in general IT certification exams, particularly those covering Linux command line, shell scripting, and text processing utilities. Exams like CompTIA Linux+, LPIC-1, and the Linux Professional Institute Certification objectives include cut as a standard text processing tool. It also appears in Red Hat Certified System Administrator (RHCSA) and Cisco DevNet Associate exams that involve automation with shell scripts.

In CompTIA Linux+ (XK0-005), cut is listed under the 'Operating Linux' domain, specifically in objectives related to using standard input and output streams, pipes, and text processing utilities. Questions may ask candidates to extract a specific field from a colon-delimited file, or to combine cut with grep to filter and format output. The exam expects familiarity with options like -d (delimiter), -f (field list), and -c (character range).

Similarly, LPIC-1 (101-500) covers cut in the 'GNU and Unix Commands' topic, where candidates must know how to use cut to extract fields from /etc/passwd or /etc/group. Exam questions often present a scenario where a candidate has a log file and needs to extract timestamps or error codes. The candidate must recall the correct syntax and options to achieve the desired output.

For the Cisco DevNet Associate (200-901), cut appears in the context of scripting and automation using shell commands to parse JSON or YAML data indirectly, or to manipulate API responses. While not a primary focus, understanding cut helps in data extraction tasks within automation scripts.

In all these exams, cut questions are typically straightforward but require attention to detail. Common pitfalls include forgetting the delimiter option (default is tab), misnumbering fields, or using -f with -c incorrectly. Candidates should practice with common delimiters (colon, comma, space) and understand how to combine cut with other commands like sed and awk. Mastering cut is a quick win for exam points in the scripting and automation sections.

## How it appears in exam questions

Cut questions appear in several patterns across certification exams. The most common is a direct syntax question: 'Given a file with lines separated by semicolons, which command extracts the third and fifth fields?' The answer is cut -d';' -f3,5 filename. These questions test recall of the -d and -f options.

Scenario-based questions are also frequent. For example: 'A system administrator needs to generate a list of usernames from /etc/passwd. Each line in /etc/passwd is colon-delimited, and the username is the first field. Which command accomplishes this?' The correct answer is cut -d':' -f1 /etc/passwd. The exam might also ask for the output when a specific line is processed, testing understanding of field numbering.

Troubleshooting-style questions present a command that fails to produce expected output. For instance: 'A user runs cut -d' ' -f2 log.txt but gets no output. The file uses multiple spaces between fields. What is the most likely cause?' The answer is that cut does not treat multiple consecutive delimiters as a single delimiter; it creates empty fields. The fix might involve using tr -s or awk.

Another question type involves combining cut with other commands. 'Which command extracts the second column from the output of the 'ps aux' command, assuming spaces are delimiters?' Here, candidates must recognize that ps output uses variable-width spacing, so cut alone may not work well. The correct approach might be using awk '{print $2}' instead. These questions test deeper understanding of tool limitations.

Exam questions also check for option usage like cut -c1-10 to extract the first ten characters, or cut -b1-5 for bytes. They might ask: 'Which option is used to specify a custom delimiter?' Answer: -d. Or 'What is the default delimiter for cut?' Answer: tab. Knowing these details is essential for quick recognition in multiple-choice formats.

## Example scenario

You are a junior system administrator tasked with a simple maintenance job. Your organization has a file called servers.txt that contains a list of server names and their IP addresses, separated by a colon, like this: 

webserver01:192.168.1.10
webserver02:192.168.1.11
dbserver01:192.168.1.20

Your supervisor asks you to create a separate file that contains only the server names (the part before the colon). This list will be used for a monitoring script that checks each server's status.

You open your terminal and navigate to the directory containing servers.txt. You remember the cut command from your training. You type: cut -d':' -f1 servers.txt > server_names.txt

You run the command and then use cat to view server_names.txt. You see:

webserver01
webserver02
dbserver01

It worked perfectly. You then need to send an alert for only the web servers, so you combine cut with grep: grep 'web' servers.txt | cut -d':' -f1 > web_servers.txt

This extracts lines containing 'web', then cuts out just the server names. You now have two clean lists without manually editing a thing.

Later, your supervisor asks for the IP addresses of all database servers. You adapt your command: grep 'db' servers.txt | cut -d':' -f2. This gives you 192.168.1.20. The whole process takes seconds, and you have avoided potential typos from manual copying. This scenario shows how cut streamlines everyday administrative tasks by extracting exactly the data you need from structured text files.

## Common mistakes

- **Mistake:** Using -f without specifying a delimiter
  - Why it is wrong: The default delimiter for cut is the tab character, not space or comma. If the file uses a different separator like a comma or colon, cut treats the entire line as a single field, producing no useful output.
  - Fix: Always use the -d option to explicitly set the delimiter to match your data. For example, use -d',' for comma-separated fields.
- **Mistake:** Assuming cut can handle multiple consecutive spaces as a single delimiter
  - Why it is wrong: Cut does not collapse multiple delimiters. Each space is a separate delimiter, so two consecutive spaces create an empty field between them. This leads to wrong field counts and incorrect output.
  - Fix: Preprocess the input with tr -s to squeeze repeated spaces into one, then pipe to cut. Or use awk which handles multiple spaces natively.
- **Mistake:** Confusing field numbers with character positions
  - Why it is wrong: Using -c (character) instead of -f (field) on delimited data extracts fixed positions, not logical columns. This works only on fixed-width input, not delimited files.
  - Fix: Check your data format. For delimited data (CSV, /etc/passwd), always use -f. For fixed-width records, use -c.
- **Mistake:** Forgetting that field numbering starts at 1
  - Why it is wrong: Some users assume field numbering begins at 0, similar to programming languages. Using -f0 returns an error or no output, and -f1 is the first field.
  - Fix: Remember that fields are 1-indexed. The first field is number 1, the second is 2, and so on.
- **Mistake:** Not escaping the delimiter correctly in some shells
  - Why it is wrong: Special characters like backslash, dollar sign, or spaces in the delimiter string can be interpreted by the shell before cut sees them, causing errors or unexpected behavior.
  - Fix: Wrap the delimiter in single quotes, e.g., -d'|'. For a literal tab, use -d$'\t' in bash or Ctrl+V Tab.

## Exam trap

{"trap":"Using cut with -d' ' (space) on a file where fields are separated by multiple spaces or tabs, and expecting it to extract the second field correctly.","why_learners_choose_it":"Learners see that the output visually has spaces between columns and assume a single space is the delimiter. They do not realize that multiple spaces create empty fields, shifting the field numbers.","how_to_avoid_it":"Always check the exact delimiter used in the data. Use cat -A or od -c to reveal hidden characters. For variable whitespace, preprocess with tr -s to collapse spaces, or switch to awk '{print $N}' which handles multiple delimiters."}

## Commonly confused with

- **cut vs awk:** Both cut and awk can extract fields from text, but awk is a full programming language with pattern matching, arithmetic, and conditional logic. Cut is simpler and faster for fixed delimiter extraction but cannot handle complex processing or variable whitespace. Use cut for quick, simple column extraction; use awk when you need more power. (Example: To extract the first column from a comma-delimited file, cut -d',' -f1 file works fine. To extract same column from a space-delimited file with irregular spacing, awk '{print $1}' file is better.)
- **cut vs sed:** Sed is a stream editor used for performing basic text transformations on an input stream. While sed can also extract pieces of text (using substitution or hold space), it is more complex for simple field extraction. Cut is purpose-built for column extraction and is more intuitive for that task. Sed is better for find-and-replace operations. (Example: To get the third field from a colon-delimited line, cut -d':' -f3 is simpler than sed 's/:/ /g' | awk '{print $3}'. But to replace all colons with commas, sed 's/:/,/g' is the best tool.)
- **cut vs grep:** Grep is used to search for lines matching a pattern, not to extract specific fields from within a line. Grep returns entire lines, while cut returns only selected portions of each line. They are often used together: grep to filter lines, then cut to extract columns from the matching lines. (Example: grep 'ERROR' logfile | cut -d' ' -f1,3 first finds lines containing 'ERROR', then extracts the first and third fields from those lines. Grep alone cannot isolate columns, and cut alone cannot filter based on content.)

## Step-by-step breakdown

1. **Identify the input source** — Determine whether the data comes from a file or standard input (e.g., from a pipe). This decides whether you pass a filename or rely on the pipe. For example, cut -d',' -f1 file.txt uses a file, while cat file.txt | cut -d':' -f2 uses standard input.
2. **Determine the delimiter** — Examine your data to see what character separates fields. Common delimiters include comma, colon, semicolon, tab, or space. Use a command like head -1 file | cat -A to show hidden characters. This step is crucial because using the wrong delimiter leads to incorrect field splitting.
3. **Choose the selection mode** — Decide whether to extract by fields (-f), characters (-c), or bytes (-b). For most structured text like CSV or configuration files, fields mode is appropriate. For fixed-width data, characters mode works. For binary or multibyte-sensitive contexts, use bytes mode.
4. **Specify the fields or positions** — List the field numbers, character ranges, or byte ranges you need. Use commas to separate individual items (e.g., -f1,3,5) and hyphens for ranges (e.g., -f2-4). Open-ended ranges like -f3- give all fields from the third onward. Remember fields are 1-indexed.
5. **Handle the output** — By default, cut prints the extracted pieces using the same delimiter as input. You can change the output delimiter with --output-delimiter if needed. Direct the output to a file using >, or to another command via pipe for further processing.

## Practical mini-lesson

The cut command is a staple in the Unix toolkit, but using it effectively requires understanding its nuances. Start by always verifying the delimiter of your data. Many learners default to -d' ' (space) when the data uses tabs or multiple spaces. Use od -c or cat -A to see exactly what characters are present. For example, a TSV file uses tabs, so the correct cut command is cut -f2 file.tsv (tab is default) or cut -d$'\t' -f2 file.tsv.

When dealing with CSV files, beware of fields containing commas inside quotes. Cut does not understand quoted fields. If a field contains a comma, cut will incorrectly split inside that field. In such cases, use a more sophisticated tool like csvkit or a scripting language with a proper CSV parser. For simple use cases without quoted commas, cut is fine.

Another practical consideration is handling headers. Many data files have a header row. To remove the header before processing, you can use tail -n +2 before cut: tail -n +2 data.csv | cut -d',' -f1,3. This prevents the header line from being included in the extraction.

In automation scripts, cut is often used to parse command output. For example, to get the PID of a specific process, you might write: ps aux | grep 'nginx' | grep -v grep | awk '{print $2}'. While awk is used here because ps output uses variable spacing, cut with tr -s can also work: ps aux | grep 'nginx' | grep -v grep | tr -s ' ' | cut -d' ' -f2. Both are valid, but awk is more readable.

A common mistake in scripts is to forget that cut treats multiple delimiters as separate. If you are processing output from commands like df or ls -l, the spacing is irregular. Always preprocess with tr -s or use awk. For example, df -h | tr -s ' ' | cut -d' ' -f5,6 extracts the usage percentage and mount point.

Finally, practice error handling. If a line has fewer fields than requested, cut prints the line as is (or nothing, depending on version). Some versions have the -s option to suppress lines without delimiters. Knowing your cut version (GNU vs. BSD) helps, as some options differ. For instance, macOS's default cut is BSD, which does not have the --output-delimiter option. On Linux, it is GNU cut with more features. When writing cross-platform scripts, stick to POSIX-compliant options to ensure portability.

## Memory tip

Think 'cut the cake, use the blade -d for delim, -f for field'.

## FAQ

**Can cut handle multiple character delimiters like '##'?**

No, cut only supports a single character as a delimiter. Use awk or sed to handle multi-character delimiters.

**What is the default delimiter for cut?**

The default delimiter is the tab character. If you do not specify -d, cut treats tabs as field separators.

**Does cut work on Windows?**

Cut is not native to Windows Command Prompt, but it is available in PowerShell (as a cmdlet) or via WSL, Cygwin, and Git Bash.

**How do I extract fields from the output of a command like 'ls -la'?**

Pipe the command to cut, but you must first squeeze spaces with tr -s. For example: ls -la | tr -s ' ' | cut -d' ' -f5,9.

**Can cut output fields in a different order than input?**

No, cut outputs fields in the order they appear in the input. To reorder fields, use awk with a print statement.

**What does the -s option do in cut?**

The -s option suppresses lines that do not contain the delimiter. It only outputs lines that have at least one delimiter.

## Summary

The cut command is a fundamental text processing utility in Unix-like systems, designed for one task: extracting specific columns or fields from structured text. Its simplicity makes it a go-to tool for quick ad-hoc analysis and for use in automation scripts. Understanding its options -d (delimiter), -f (field list), -c (character positions), and -b (byte positions) is essential for anyone working with command-line data.

In exams, cut appears as a straightforward but detail-oriented topic. Candidates must remember that fields are 1-indexed, the default delimiter is tab, and that cut does not handle multiple delimiters gracefully. Combining cut with other commands like grep, sort, and tr is a frequent exam scenario.

For IT professionals, cut is a time-saving tool that streamlines tasks like parsing log files, extracting user data from system files, and transforming command outputs for reporting. While it has limitations, such as no support for quoted fields or multi-character delimiters, its speed and simplicity make it irreplaceable in many everyday tasks. Mastering cut is a small but significant step toward becoming proficient with the Unix command line and shell scripting.

---

Practice questions and the full interactive page: https://courseiva.com/glossary/cut
