Skip to content

Instantly share code, notes, and snippets.

@Nikita240
Last active March 23, 2016 03:31
Show Gist options
  • Save Nikita240/98bcebb33c919a3f5c25 to your computer and use it in GitHub Desktop.
Save Nikita240/98bcebb33c919a3f5c25 to your computer and use it in GitHub Desktop.
Splits a Name column in a .csv file into FirstName and LastName
# *******************************USAGE*******************************
# cat names.csv | ./name.sh > names-processed.csv
target_col=3
new_title="FirstName,LastName,"
#pass the header line through the buffer first
read header
title=$(echo $header | sed -r "s/([^,]*,){$target_col}.*$/\1/")
echo $header | sed -r "s/$title/$new_title/"
#split name into Fname and Lname
while read row
do
name=$(echo $row | sed -r "s/([^,]*,){$target_col}.*$/\1/")
split_name=$(echo $name | sed -r "s/^(\S+)\s(.*)/\1,\2/")
echo $row | sed -r "s/$name/$split_name/"
done
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment