How to remove sections from each line of files in the Linux system?

In this article, we will learn to remove sections from each line of files in the Linux/Unix operating system using the cut command.

The cut command is used to extract and print selected portions of each line from files. It allows you to cut specific sections of a line by byte position, character position, or field delimiters. This makes it particularly useful for processing structured text data, CSV files, and extracting specific columns from formatted output.

Syntax

The general syntax of the cut command is as follows −

cut OPTION... [FILE]...

Options

Here are the main options available with the cut command −

Option Description
-b, --bytes=LIST Select only specified bytes
-c, --characters=LIST Select only specified characters
-d, --delimiter=DELIM Specify delimiter instead of TAB
-f, --fields=LIST Select only specified fields
-n With -b option, do not split multibyte characters
--complement Complement the set of selected bytes, characters, or fields
-s, --only-delimited Do not print lines not containing delimiters
--help Display help and exit
--version Output version information and exit

Examples

Cutting Specific Bytes

To cut specific bytes from each line, use the -b option. This example extracts bytes 1 and 2 from each line −

$ cut -b 1,2 text1.txt
ar
ry
sa
st
to
ut
va

Cutting Specific Characters

To cut specific characters from each line, use the -c option. This example extracts characters 1, 2, and 4 from each line −

$ cut -c 1,2,4 text1.txt
ara
ryn
sa
stv
to
uta
vah

Cutting Character Ranges

To cut a range of characters, specify the range with the -c option. This example extracts characters 2 through 4 −

$ cut -c 2-4 text1.txt
rya
yan
am
tev
om
tka
ash

Cutting Fields with Delimiters

To cut specific fields from delimited data, use the -f option with -d to specify the delimiter −

$ cut -d ':' -f 1,3 /etc/passwd
root:0
daemon:1
bin:2
sys:3

Common Use Cases

  • CSV Processing − Extract specific columns from comma-separated files

  • Log Analysis − Extract timestamps or specific fields from log files

  • System Information − Parse output from system commands like ps, ls, or /etc/passwd

  • Text Processing − Remove unwanted portions of text files

Key Points

  • Use -c for character positions, -b for byte positions, and -f for field positions

  • Ranges can be specified as 1-5, lists as 1,3,5, or combinations like 1-3,5

  • Default delimiter is TAB; use -d to specify custom delimiters

  • The --complement option selects everything except the specified positions

Conclusion

The cut command is a powerful tool for extracting specific sections from text files in Linux. Whether working with structured data, logs, or system files, it provides precise control over which portions of each line to extract, making it essential for text processing and data manipulation tasks.

Updated on: 2026-03-17T09:01:38+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements