Sometimes you may need to display specific columns from your files or command output in Linux. You can easily do this using awk command. In this article, we will learn how to display specific columns in Linux.
How to Display Specific Columns in Linux
We will look at different use cases to display specific columns in Linux. Let us say you get the following output when you run ls command in your present working directory.
$ ls -l -rw-r--r-- 1 ubuntu ubuntu 200M Apr 27 22:04 file1.txt -rw-r--r-- 1 ubuntu ubuntu 400M Apr 27 22:04 file2.txt -rw-r--r-- 1 ubuntu ubuntu 500M Apr 27 22:04 file3.txt -rw-r--r-- 1 ubuntu ubuntu 600M Apr 27 22:04 file4.txt -rw-r--r-- 1 ubuntu ubuntu 700M Apr 27 22:04 file5.txt
Let us say you save this output in a file data.txt.
1. Display Single Column
Here is the command to display 5th column from file data.txt, using awk command.
$ awk '{print $5}' data.txt 200M 400M 500M 600M 700M
In the above command awk will parse the file into columns using the space character as delimiter, and then extract the specified column for you, as per your requirement.
You can use awk command directly on ls command output as shown below.
$ ls -l | awk '{print $5}' 200M 400M 500M 600M 700M
Please note, in awk command, the numbering of columns starts from 1 and not 0. The first column is 1 and the second column is 2 and so on.
2. Display Multiple Columns
If you want to display multiple columns, say, column 5 and 9, then here is the command to do so. We simply prefix each column’s index with $ sign and use print command to display it in our desired format.
$ awk '{print $5 " " $9}' data.txt 200M file1.txt 400M file2.txt 500M file3.txt 600M file4.txt 700M file5.txt
Here is the above command using the output of ls command.
$ ls -l | awk '{print $5 " " $9}' 200M file1.txt 400M file2.txt 500M file3.txt 600M file4.txt 700M file5.txt
3. Display Range of Columns
If you want to display range of columns 3-8, you can use the following command. In this case, we simply loop through the column and construct our desired output.
$ awk '{ for (i = 3; i <= 8; ++i) printf $i" "; print ""}' data.txt ubuntu ubuntu 200M Apr 27 22:04 ubuntu ubuntu 400M Apr 27 22:04 ubuntu ubuntu 500M Apr 27 22:04 ubuntu ubuntu 600M Apr 27 22:04 ubuntu ubuntu 700M Apr 27 22:04
In the above command, we use for loop to go through the columns and use print command to print the output in standard output.
You can use the above command with the output of ls command also.
$ ls -l | awk '{ for (i = 3; i <= 8; ++i) printf $i" "; print ""}' ubuntu ubuntu 200M Apr 27 22:04 ubuntu ubuntu 400M Apr 27 22:04 ubuntu ubuntu 500M Apr 27 22:04 ubuntu ubuntu 600M Apr 27 22:04 ubuntu ubuntu 700M Apr 27 22:04
You can also use the cut command to get the same output below.
$ cut -d' ' -f3-8 input.txt ubuntu ubuntu 200M Apr 27 22:04 ubuntu ubuntu 400M Apr 27 22:04 ubuntu ubuntu 500M Apr 27 22:04 ubuntu ubuntu 600M Apr 27 22:04 ubuntu ubuntu 700M Apr 27 22:04
4. Change Awk’s field separator
By default, awk command uses whitespace characters as field separators. If your file or input data has a different field separator, such as comma, then you can use the -F option to specify the field separator, as shown below.
Let us say you have a .csv file as shown below.
$ cat input.txt -rw-r--r--,1,ubuntu,ubuntu,200M,Apr 27 22:04,file1.txt -rw-r--r--,1,ubuntu,ubuntu,400M,Apr 27 22:04,file2.txt -rw-r--r--,1,ubuntu,ubuntu,500M,Apr 27 22:04,file3.txt -rw-r--r--,1,ubuntu,ubuntu,600M,Apr 27 22:04,file4.txt -rw-r--r--,1,ubuntu,ubuntu,700M,Apr 27 22:04,file5.txt
Here is the command to print fields 5, 6, 7 in above input file.
$ awk -F"," '{print $5 " " $6 " " $7}' data.txt 200M Apr 27 22:04 file1.txt 400M Apr 27 22:04 file2.txt 500M Apr 27 22:04 file3.txt 600M Apr 27 22:04 file4.txt 700M Apr 27 22:04 file5.txt
As you can see, since we have specified space delimiter in awk command, it outputs the 3 columns separated by space instead of using comma.
5. Skip the first line
Often, while displaying columns, you may need to skip the header or first line of your file, or input data. In such cases, use the following command. awk command stores line number in NR variable. We can easily use if condition to check line number and print the output only if line number if not 1.
$ awk '{if (NR!=1) {print}}' data.txt
Here is the command to print columns 5, 7 from data.txt file, after skipping first line.
$ awk '{if (NR!=1) {print $5 " " $9}}' data.txt
Please remember to enclose the if condition within curly braces {}, which in turn, are within single quotes. Also, the print statement must also be within the if condition’s curly braces. Only then it will be within the scope of if statement.
In this article, we have learnt several ways to display specific columns in Linux. You can use these commands on almost every Linux system since they are universally available.
Also read:
How to List Active Connections in PostgreSQL
How to Create Swap Space in Ubuntu/Debian
How to Fix “mv : Argument List too Long”
How to Repair MySQL Databases & Tables
How to Optimize MySQL Tables
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.