Display specific columns of a file in Linux?

In Linux system administration and data processing, displaying specific columns from text files is a fundamental task. Whether you're analyzing log files, processing CSV data, or extracting information from command outputs, knowing how to select and display specific columns efficiently is essential.

This tutorial covers various methods to display columns using the awk and cut commands, two powerful text processing tools available in all Linux distributions.

Display Single Column

Let's start with a sample file containing the output of the ls -l command in long listing format:

$ cat input.txt
-rw-r--r-- 1 jarvis jarvis 200M Nov 27 22:04 file1.dat
-rw-r--r-- 1 jarvis jarvis 400M Nov 27 22:04 file2.dat
-rw-r--r-- 1 jarvis jarvis 500M Nov 27 22:04 file3.dat
-rw-r--r-- 1 jarvis jarvis 600M Nov 27 22:04 file4.dat
-rw-r--r-- 1 jarvis jarvis 700M Nov 27 22:04 file5.dat

Using awk Command

We can display the fifth column (file sizes) using the awk command:

$ awk '{print $5}' input.txt
200M
400M
500M
600M
700M

The awk command uses the following syntax:

  • print Built-in function that outputs text to standard output

  • $5 Represents the 5th column (field) in each line

  • $N General format where N represents any column number

Using cut Command

You can achieve the same result using the cut command:

$ cut -d' ' -f5 input.txt
200M
400M
500M
600M
700M

The cut command options:

  • -d Specifies the field delimiter (space character in this case)

  • -f5 Selects the 5th field

Display Multiple Columns

To display multiple columns simultaneously, you can specify multiple field numbers.

Using awk for Multiple Columns

Display filename and file size together:

$ awk '{print $9 " " $5}' input.txt
file1.dat 200M
file2.dat 400M
file3.dat 500M
file4.dat 600M
file5.dat 700M

Here, $9 represents the filename from the 9th column, and we concatenate it with the file size using a space separator.

Using cut for Multiple Columns

With cut, specify multiple fields separated by commas:

$ cut -d' ' -f9,5 input.txt
200M file1.dat
400M file2.dat
500M file3.dat
600M file4.dat
700M file5.dat

Note that cut displays columns in the order they appear in the file, not in the order specified in the command.

Display Range of Columns

When dealing with files containing many columns, you can display a range of consecutive columns.

Using awk with Loop

Display columns 3 through 8 using a loop:

$ awk '{ for (i = 3; i <= 8; ++i) printf $i" "; print ""}' input.txt
jarvis jarvis 200M Nov 27 22:04 
jarvis jarvis 400M Nov 27 22:04 
jarvis jarvis 500M Nov 27 22:04 
jarvis jarvis 600M Nov 27 22:04 
jarvis jarvis 700M Nov 27 22:04 

This command uses:

  • for loop Iterates through columns 3 to 8

  • printf Prints formatted output without automatic newlines

Using cut with Range

Display the same range using cut with hyphen notation:

$ cut -d' ' -f3-8 input.txt
jarvis jarvis 200M Nov 27 22:04
jarvis jarvis 400M Nov 27 22:04
jarvis jarvis 500M Nov 27 22:04
jarvis jarvis 600M Nov 27 22:04
jarvis jarvis 700M Nov 27 22:04

Changing Field Separators

Both awk and cut can work with different field delimiters. Let's modify our input file to use commas as separators:

$ cat input.txt
-rw-r--r--,1,jarvis,jarvis,200M,Nov 27 22:04,file1.dat
-rw-r--r--,1,jarvis,jarvis,400M,Nov 27 22:04,file2.dat
-rw-r--r--,1,jarvis,jarvis,500M,Nov 27 22:04,file3.dat
-rw-r--r--,1,jarvis,jarvis,600M,Nov 27 22:04,file4.dat
-rw-r--r--,1,jarvis,jarvis,700M,Nov 27 22:04,file5.dat

Extract filename, size, and timestamp using comma as delimiter:

$ awk -F"," '{print $7 " " $5 " " $6}' input.txt
file1.dat 200M Nov 27 22:04
file2.dat 400M Nov 27 22:04
file3.dat 500M Nov 27 22:04
file4.dat 600M Nov 27 22:04
file5.dat 700M Nov 27 22:04

The -F option in awk specifies the field separator. Notice that column 6 (timestamp) contains spaces but no commas, demonstrating how field separators work.

Comparison

Feature awk cut
Column Reordering Yes, flexible ordering No, maintains file order
Multiple Delimiters Yes, regex patterns Single character only
Calculations Yes, arithmetic operations No
Performance Slower for simple tasks Faster for basic extraction
Complexity More powerful, complex syntax Simple, limited functionality

Conclusion

Both awk and cut are essential tools for extracting specific columns from text files in Linux. Use cut for simple column extraction tasks and awk when you need more flexibility, column reordering, or text processing capabilities. Understanding these commands greatly enhances your ability to process and analyze data efficiently in Linux environments.

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

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements