How to extract data from a column in a text file using Linux command line?
Published on Aug. 22, 2023, 12:17 p.m.
To extract data from a column in a text file using the Linux command line, you can use the cut
command. Here’s an example:
Assuming we have a text file named data.txt
with three columns of data, separated by tabs:
hello world 123
foo bar 456
To extract the second column, you can use the cut
command as follows:
cut -f 2 data.txt
This will output:
world
bar
The -f
option specifies the field or column to extract, and 2
refers to the second column. You can specify multiple fields or columns by separating them with commas. If your file is delimited by a character other than a tab, you can specify a different delimiter using the -d
option, like this:
cut -d ',' -f 2 data.csv
This command will extract the second column from a CSV file named data.csv
using a comma delimiter.