Otorrinolaringologista -man Otorrinolaringologista -man - 3 months ago 10
Bash Question

Sending a file's data to multiple other files in Shell

I'm trying to send the data from a file to multiple other files based on the Date of the following database:

100:Clinton Mcdaniel:16/04/2016
101:Patience Mccarty:18/03/2013
102:Carol Holman:24/10/2013
103:Roth Lamb:11/02/2015
104:Chase Gardner:14/06/2014
105:Jacob Tucker:05/11/2013
106:Maite Barr:24/04/2014
107:Acton Galloway:18/01/2013
108:Helen Orr:10/05/2014
109:Avye Rose:07/06/2014
110:Xerxes Richmond:13/11/2015
111:Illana Campos:22/07/2016
112:Ruby Baker:21/01/2015
113:Mark Lynn:21/06/2014
114:George Tyson:03/10/2014
115:Heather Sweeney:26/11/2015
116:Zahir Hines:08/10/2014
117:Cleo Decker:12/06/2014
118:Cyrus Best:07/01/2015
119:Dominique Goodwin:19/12/2015
171:Laura Mercado:06/06/2013
165:Kai Nash:16/07/2015
184:Oren Flores:27/09/2013
123:Rogan Rowe:05/12/2014
146:Vivian Stevens:20/01/2014
130:Anne Lawson:23/04/2015
196:Willow Powers:11/11/2015
167:Megan Caldwell:04/05/2016
195:Megan Long:03/10/2015
126:Castor Rhodes:19/06/2015
190:Anne Sullivan:12/01/2013
159:Lars Keith:28/10/2015


What i want to do is to send each line to an specific file depending on the year, if the lines has they year 2013 on the date i want to add it to a file named Year2013.txt, if the date colum has 2014 than i want it to go to the file Year2014.txt, etc etc.

Ive been trying to use the if statement together with AWK but it isnt working as i expected. It usually only saves the year or the date but not the entire line of the database.

I tried codes like

$awk -F":" '{if($3.7==2013)print $0 > "Year2013.txt"}}' IDNameDate


But they usually don`t work

Answer

here you go...

$ awk -F/ '{print > "Year"$NF".txt"}' file

$ head -3 Year*.txt
==> Year2013.txt <==
101:Patience Mccarty:18/03/2013
102:Carol Holman:24/10/2013
105:Jacob Tucker:05/11/2013

==> Year2014.txt <==
104:Chase Gardner:14/06/2014
106:Maite Barr:24/04/2014
108:Helen Orr:10/05/2014

==> Year2015.txt <==
103:Roth Lamb:11/02/2015
110:Xerxes Richmond:13/11/2015
112:Ruby Baker:21/01/2015

==> Year2016.txt <==
100:Clinton Mcdaniel:16/04/2016
111:Illana Campos:22/07/2016
167:Megan Caldwell:04/05/2016
Comments