ali balador
ali balador

Reputation: 21

extracting data from a file with awk

I have a data set like below

first 0 1
first 1 2
first 2 3
second 0 1
second 1 2
second 2 3
third 0 1 
third 1 2
third 2 3

I need to check this file and extract the third columns for first, second and third and store them in different files.

The output files should contain:

1
2
3

Upvotes: 2

Views: 784

Answers (1)

Chris Seymour
Chris Seymour

Reputation: 85785

This is pretty straight forward awk '{print $3>$1}' file i.e. print the third field and redirect the output to the file, where the filename is the first field.

Demo:

$ ls 
file

$ awk '{print $3>$1}' file

$ ls
file  first  second  third

$ cat first
1
2
3

$ cat second 
1
2
3

$ cat third 
1
2
3

Upvotes: 9

Related Questions