0

I have a data set as follow:

Cow_ID  Age DIM
1   1424    0
1   1425    1
1   1426    2
1   1427    3
2   1428    4
2   1429    5
2   1430    6
2   1431    7
3   1432    8
3   1433    9
3   1434    10
3   1435    11
3   1436    12
4   1437    13
4   1438    14
4   1439    15

I want to use awk to subset this data according to Cow_ID and write it to a file. Especially, each file also contain the header as shown in the big data set. I am using this at this moment but it does not work:

awk 'Cow_ID=1 {print}' > data1.txt

This is the example output I want to have:

data1:

Cow_ID  Age DIM
1   1424    0
1   1425    1
1   1426    2
1   1427    3

Data2:

Cow_ID  Age DIM
2   1428    4
2   1429    5
2   1430    6
2   1431    7

and so forth.

marked as duplicate by don_crissti, jw013, EightBitTony, jasonwryan, taliezin Mar 2 '16 at 22:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    could you please provide a sample output that you are expecting to get ? – MelBurslan Mar 2 '16 at 22:29
  • hi Melburslan, I posted my example outputs format in the answer post below – hieu Mar 2 '16 at 22:33
0

This seems to cover your requirement. Shows lines where NR (record number) is 1, i.e. the header line plus any lines where first field is "1".

awk 'NR == 1 || $1 == "1"' file.txt
Cow_ID  Age DIM
1   1424    0
1   1425    1
1   1426    2
1   1427    3
awk 'NR == 1 || $1 == "2"' file.txt
Cow_ID  Age DIM
2   1428    4
2   1429    5
2   1430    6
2   1431    7

Not the answer you're looking for? Browse other questions tagged or ask your own question.