<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Oct 6, 2015, at 18:57, Keith Larson <<a href="mailto:klarson@k12group.net" class="">klarson@k12group.net</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">
<meta name="Generator" content="Novell Groupwise Client (Version 14.0.2 Build: 120664)" class="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" class="">
<div style="font: 10pt/normal Segoe UI; font-size-adjust: none; font-stretch: normal;" class=""><div class="GroupWiseMessageBody" id="GroupWiseSection_1444175483000_klarson@k12group.net_59511A0513870000BD1BF681F57056B0_"><div class="">I think that awk is the right tool, but I'm open to a better solution if there is one.</div><div class=""><br class=""></div><div class="">I have a .csv format file that I want to split into two files based on the value found in one of the columns. For discussion purposes, say that the file has several different values about a person. FirstName, LastName, Phone, DOB and Gender. The file does have a header row. I would like two files, one with all entries where gender is M and another file where gender is F. I would like the header row in both files if possible. The key is that I want the row to be kept completely intact with the delimiters remaining in place.</div><div class=""><br class=""></div><div class="">Any suggested solutions? Ideally, I want something that can be done in a bash script so that I can run this on a nightly schedule.</div></div></div></div></blockquote><div><br class=""></div><div>awk is generally the right tool for field delimited, data, yes. But going by what you suggest the file looks like</div><div><br class=""></div><div>John,Smith,6145551212,01011980,M</div><div>Lucy,Jones,6145551213,01011980,F</div><div><br class=""></div><div>there’s a simple solution if you’re ensured that a single letter at the end of the line is your gender</div><div><br class=""></div><div>grep ‘,F$’ source.csv > females.csv</div><div>grep ‘,M$’ source.csv > males.csv</div><div><br class=""></div><div>If you need to do more sophisticated processing based on the fields, you can do something like this on each line (watch the IFS / whitespace issues with $line):</div><div><br class=""></div><div>gender=$(echo “$line" | awk -F, {print $4})</div><div>case $gender in</div><div> M)</div><div> # do something here</div><div> echo $line >> $males</div><div> F)</div><div> # do something else here</div><div> echo $line >> $females</div><div>esac</div><div><br class=""></div><div><br class=""></div><div><br class=""></div><div>… or what Jeff suggested.</div></div></body></html>