colrm Command in Linux



The colrm command stands for "column remove" and is used in Linux to manipulate text, source code, or script files. It is used to remove specific columns from a file, where a column represents a single character in a line. This command is useful in text-processing tasks where specific parts of each line need to be removed based on their column positions.

In this tutorial, we’ll discuss various use cases of the colrm command in Linux using practical examples.

colrm Command in Linux

The colrm command starts from column index 1, not 0. It can accept input from standard input and show the output on standard output. It can also read the input from a file and output the result to standard output. It removes the columns according to the provided column numbers.

Basic Syntax of colrm Command

The colrm command can be executed with single or two arguments. If executed with only one argument, it removes from the specified start column to the end of each line. On the other hand, if two columns are provided, it removes the columns between the specified start and end column numbers(both inclusive). The basic syntax to use the colrm command in Linux is shown in the following snippet −

colrm [startCol [endCol]]

You can use the following syntax to remove columns from a file −

colrm startCol endCol < fileName

To remove only a single column, you must specify the same startCol and endCol.

colrm Command Options

The table below illustrates all the valid options that the colrm command accepts −

Option Description
startCol It represents a column number to start removing from.
endCol It represents a column number to stop removing at().
-V, --version It retrieves the version of the colrm command.
-h, --help It shows the basic usage of the command, valid options, and their descriptions, and a brief description of what the command does.

Access the Colrm Command Manual Page

Do you want to learn more about the colrm command? Simply type “man colrm” in the terminal and hit the ENTER key, as a result, you will see the command's description, its valid options, and many more details.

man colrm
Access Colrm Command Manual Page

Access the Colrm Command Help Page

Similarly, you can execute the colrm command with the -h option to understand the basic usage of colrm command −

colrm -h
Access Colrm Command Help Page

Check colrm Version

Run the colrm command with the “-V” or “--version” option to see its version information −

colrm -V
Check colrm Command Version

Install Colrm Command in Linux

The colrm command belongs to the “util-linux” package and is pre-installed on most Linux distributions. However, if for some reason it’s not installed on your system, you can install it using your package manager. Let’s learn how to install colrm command on different Linux distributions −

#for installing colrm on Debian-based systems
sudo apt install bsdextrautils

#for installing colrm on Fedora
sudo dnf install util-linux

#for installing colrm on CentOS or RHEL
sudo yum install util-linux

#for installing colrm on Arch Linux
sudo pacman -S util-linux

Examples of colrm Command in Linux

Let’s consider some examples to understand how the colrm command works in Linux −

  • How to Remove Columns between a Specific Range?
  • How to Remove Columns from a Specific Column to the End?
  • How to Remove Only One Column Using colrm Command?
  • How to Use Colrm Command to Remove Columns from a File?
  • How to Remove Columns from Standard Input at Run Time?

How to Remove Columns between a Specific Range?

We need to specify the starting and ending range in the colrm command to remove columns between the selected range. For instance, the following command removes the columns from the input between 7 and 9 −

echo "Hello Welcome to tutorialspoint.com" | colrm 7 9

The output demonstrates that the columns between 7 and 9 (both inclusive) have been removed from the input −

Remove Columns between Specific Range

How to Remove Columns From a Specific Column to the End?

If you want to remove columns from a specific starting point to the last column, specify only the starting column number. For instance, the following command removes columns from column 14 to the end −

echo "Hello Welcome to tutorialspoint.com" | colrm 14
Remove Columns From Specific Column to End

How to Remove Only One Column Using colrm Command?

Execute the colrm command with the same starting and ending column number to remove only one column from the input. For example, the below-mentioned command removes the first column from the input −

echo "Hello Welcome to tutorialspoint.com" | colrm 1 1
Remove One Column Using Colrm Command

How to Use colrm Command to Remove Columns From a File?

We have a text file named “exampleFile.txt” that contains the following content −

cat exampleFile.txt
Remove Columns From a File

Suppose we want to remove the first two columns of the file. To do this, we must execute the colrm command as follows −

colrm 1 2 < exampleFile.txt 

The output confirms that the colrm command successfully removes the first two columns from each row −

Confirm Remove Columns From a File

How to Remove Columns from Standard Input at Run Time?

Run the colrm command with the specified starting and ending column range. It prompts for input during runtime, removes the specified columns from the input, and generates the output −

colrm 2 4

The output demonstrates that the colrm command accepts input, removes selected columns, and outputs the result. This process continues until you press CTRL+C to return to the terminal −

Remove Columns from Standard Input

This is how the colrm command works in Linux.

Conclusion

The colrm (column remove) command is a useful tool in Linux that manipulates text, source code, or script files by selectively removing specified columns. In this article, we explored several examples to demonstrate how to remove columns within specified ranges, from a specific column to the end of a line, or singularly remove columns. We also explained how to interactively remove columns from input, a specific file, or standard input.

Advertisements