Linux colrm Command Tutorial for Beginners (with Examples)

If you primarily work on the Linux command line, text editing is likely one of the most frequent tasks that you may find yourself involved in. Whether it’s editing scripts, source code files, configuration files, or a regular text file, there are several text editing tools that help you in your work.

One such tool is colrm, which we will be discussing here using some easy to understand examples. But before we start with that, it’s worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.

Linux colrm command

The colrm command in Linux lets you remove selected columns from a file. Following is its syntax:

colrm [start [stop]]

And here’s how the colrm man page describes what the tool does:

 The colrm utility removes selected columns from the lines of a file.  A
     column is defined as a single character in a line.  Input is read from
     the standard input.  Output is written to the standard output.

     If only the start column is specified, columns numbered less than the
     start column will be written.  If both start and stop columns are speci?
     fied, columns numbered less than the start column or greater than the
     stop column will be written.  Column numbering starts with one, not zero.

Following are some Q&A-styled examples that should give you a good idea on how the colrm command works.

Q1. How to use colrm command?

As already mentioned, the colrm command lets you remove columns. So you just need to specify a numeric value, and column corresponding to that value as well as all the subsequent columns will get axed in the output.

For example, here’s a text file ‘file1.txt’ that contains the following text:

abcdefgh

For colrm, each alphabet represents a column. So now, suppose the requirement is to delete column 6 and all susequent columns, then this can be done using:

colrm 6 < file1.txt

Following is the output the above command will produce:

colrm command usage

So you can see that columns 6, ,7 and 8 were axed.

Q2. How to remove only a single column?

In that case, you need to specify both start and stop columns. Suppose the requirement is to delete only column number 6, then for that, you need to execute the colrm command in the following way:

colrm 6 6 < file1.txt

Here’s the output this command produced on my system:

How to remove only a single column

So you can see only the 6th column was deleted. Needless to say, you can delete a range of columns this way. For example:

colrm 2 6 < file1.txt

This command will delete columns ranging from 2 to 6. It’s worth keeping in mind that the column numbering starts from 1, not zero.

Q3. How colrm handles backspaces and tabs?

The tool’s man page explains this quite clearly. Here’s the relevant excerpt:

 Tab characters increment the column count to the next multiple of eight.
     Backspace characters decrement the column count by one.

Also, keep in mind that colrm returns 0 on success, and >0 if an error occurs.

Q4. How to make colrm accept input from stdin?

That’s very easy. You don’t have to do anything extra – just provide column number (or range) in input.

Here’s an example:

How to make colrm accept input from stdin

So the command waits for you to provide the input at run time, and then produces otutput after you press the enter key.

Conclusion

As you’ll likely agree, the colrm command is a useful little utility when it comes to removing columns from a text file. Here, in this tutorial, we have discussed almost all of its features. Once you are done practicing these, you can learn more about the command by heading to its man page.

Himanshu Arora

About Himanshu Arora

Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu’s work has also been featured in some of world’s other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.

Share this page:

linux colrm command tutorial for beginners with examples 3
linux colrm command tutorial for beginners with examples 4
linux colrm command tutorial for beginners with examples 5
linux colrm command tutorial for beginners with examples 6

نوشته های مشابه