How to Remove Last N Columns of File in Linux

A great way to remove the last n columns of a file in Linux is to use the combination of the cut and rev commands.

The cut command is used to cut/remove sections/columns of text files. The rev command is used the reverse the content of lines in text files. In order to use these commands together to remove the last n columns of a file in Linux, you must:

1. Use rev to reverse the file
2. Use cut to remove columns from the beginning of the reversed text
3. Use rev to reverse the file into its original format (excluding the removed columns)

Here is an example removing the last 2 columns from sample_data_1.txt:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">