158 Words

Extracting a Specific Column in the Linux CLI

Sometimes you may need to extract a specific column (or tab-separated field) from a file in the Linux command line.

Example File

Let’s say we have a file (file.txt) with the following content:

A  B  C  
D  E  F  
G  H  I  

Extracting the First Column

To extract the first column from each line, you can use awk:

awk '{print $1}' file.txt

Output:

A  
D  
G  

Extracting the Second Column

To extract the second column, modify the command:

awk '{print $2}' file.txt

Output:

B  
E  
H  

No Need for cat

Using cat file | awk ... is unnecessary; awk can read the file directly. Always prefer:

awk '{print $N}' file.txt

where N is the column number you want to extract.

This method works for both space- and tab-separated files. If your file uses a different delimiter (like commas), you can specify it with -F, e.g.:

awk -F',' '{print $2}' file.txt