Skip to content

Instantly share code, notes, and snippets.

@ysinc88
Created December 3, 2015 15:00
Show Gist options
  • Save ysinc88/f916eda91f569ef9dcbf to your computer and use it in GitHub Desktop.
Save ysinc88/f916eda91f569ef9dcbf to your computer and use it in GitHub Desktop.
#!/bin/bash
datafile=$1
ctr=0;
while read line
do
# counter to keep track of line number
ctr=$((ctr + 1))
# skip header line for processing
if [[ $ctr -gt 1 ]];
then
# create filename using date field present in record
vdate=${line%% *}
vday1=${vdate%%/*}
vday=`printf "%02d" $vday1` # day with padding 0
vyear=${vdate##*/} # year
vfilename="$2-${vyear}${vday}.txt" # filname in YYYYMM.txt format
# check if file exists or not then put header record in it
if [ ! -f $vfilename ]; then
head -1 $datafile > $vfilename
fi
# put the record in that file
echo "$line" >> $vfilename
fi
done < $datafile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment