Display specific columns of a file in Linux?


Overview

We often perform various file operations on our Linux systems. One of the most common operations is to display certain columns from a text document.

Here, we’ll cover the different methods for achieving this.

Display Single Column

Let's create a new folder for our example. The input.txt files contain the output of the ls -l command in the long listing (long) 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

We can display specific columns using the awk command. Let's read the fifth column from the file.

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

Let's look at the options we used in the awc command −

  • print − it’s awk’s built-in function which prints text to the standard output stream

  • $5 − it represents files size from the 5th column

Note that awk has $N to represent the nth column. For example, $1 represents the 1st row.

You can also display specific columns using the cut command. Let's print the same column by cutting out the first two lines.

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

We're going to take a quick peek at the option we used when cutting out a word from our text file.

  • −d − it represents the field delimiter. Its default value is a tab character

  • −f5 − it represents the file size from the 5th column

Display Multiple Columns

We can use awks to display multiple rows as well. Let's print the filename and its length −

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

We're going to use the following options for our awk command −

  • $9 − it represents file name from the 9th column

You can also display multiple columns using the cut command. For example, we could specify multiple column names by listing them separated by commas as follows −

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

You cannot reorder columns using the “Cuts” command. Input is selected in the same order that they're presented.

Display Range of Columns

It may sometimes be convenient to write a loop when displaying columns in large amounts. Let's print all rows from column 3 through row 8.

$ 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

We'll now examine the options we've been using in our awk command.

  • for − it’s a looping construct of the awk

  • printf − it’s awk’s built-in function which prints formatted text to the standard output stream

You can use the cut command for the same effect. You can use hyphens to indicate ranges of values for each column. For example, if you want to select rows where the value of one of the columns falls between 1 and

$ 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 awk‘s Field Separator

By default, awk uses a single space character as a column separator. We can change it however we want. Let's start by modifying our original input file so that we replace any space characters with comma characters. Now the modified version of the input text looks like this −

$ 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

Let's print the filename, its size, and time stamp using commas as a column separators.

$ 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

Let's look at the options we used in the awc command −

  • −F − it represents the field separator

Notice that in the sixth column, there are no commas between the fields. This demonstrates the use of field separators.

Conclusion

We looked at several different ways to show specific columns from a text document. These commands can be used in everyday situations when working with the Linux system.

Updated on: 26-Dec-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements