Linux cut command

Linux cut command

Linux cut Command – Extract Columns and Fields from Text

The cut command in Linux is used to extract specific sections (columns or fields) of data from a file or input. It's especially useful for processing delimited data, such as CSV files, logs, or text files with fixed-width fields.

Syntax of cut

cut [OPTIONS] [FILE]
  • OPTIONS → Flags that define how to cut the data (columns, delimiters, etc.).
  • FILE → The file from which to extract data (if omitted, cut reads from standard input).

Commonly Used cut Options

1. Cut by Character Position (-c)

To extract specific characters from each line:

cut -c 1-5 filename

Example:

cut -c 1-5 data.txt

This will show the first 5 characters from each line of data.txt.

2. Cut by Field Using a Delimiter (-f)

To extract specific fields separated by a delimiter (e.g., comma or tab):

cut -d 'delimiter' -f field_number filename
  • -d 'delimiter' → Specifies the delimiter (default is tab).
  • -f field_number → Specifies which field(s) to extract (fields are 1-indexed).

For example, extracting the second field from a comma-separated file:

cut -d ',' -f 2 data.csv

This will print the second field from each line of data.csv.

3. Cut Multiple Fields (-f with Multiple Fields)

To extract multiple fields, use commas:

cut -d ',' -f 1,3 data.csv

This prints the first and third fields of data.csv.

To extract a range of fields:

cut -d ',' -f 2-4 data.csv

This extracts fields 2 to 4 (inclusive) from each line.

4. Cut from Fields with Specific Byte Position (-b)

To extract a specific byte range from each line (useful for fixed-width files):

cut -b 1-5 filename

This extracts bytes 1 to 5 from each line of filename.

5. Cut Fields from Standard Input (Piped Data)

You can pipe the output of a command into cut to extract data:

echo "apple,orange,banana" | cut -d ',' -f 2

This will output:

orange

6. Display the First n Characters (-c)

To display the first n characters of each line:

cut -c 1-10 filename

This will show characters from position 1 to 10 for each line in filename.

7. Cut Fields from a Tab-Delimited File (Default Delimiter)

If the fields in a file are tab-separated (the default delimiter), you don’t need to specify -d:

cut -f 1 filename

This extracts the first tab-separated field from each line in filename.

8. Show All Fields (-f with All Fields)

To display all fields from a delimited file, you can omit -f (or specify all fields):

cut -d ',' -f 1- filename

This will display all fields from filename where fields are separated by commas.

Examples

1. Extract Specific Columns from a CSV File

Given a file data.csv with the following content:

Name,Age,Location Alice,30,New York Bob,25,Los Angeles Charlie,35,Boston

To extract the Name and Location columns:

cut -d ',' -f 1,3 data.csv

Output:

Name,Location Alice,New York Bob,Los Angeles Charlie,Boston

2. Extract Data from a Log File

Given a log file with tab-delimited fields, to extract the first and third fields:

cut -f 1,3 log.txt

Difference Between cut and awk

CommandPurpose
cutExtracts data by character position or delimiter.
awkMore flexible text processing tools that can handle complex operations.

Conclusion

The cut command is a simple but effective tool for extracting specific data portions from text files. It is particularly useful for quick data extraction, especially when working with delimited data files.

Would you like additional details or SEO optimization? 🚀

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close