#Check if the argument is numeric
isNumeric()
{
numericExpression='^[0-9]+$'
if ! [[ $1 =~ $numericExpression ]]
then
echo 1;
else
echo 0;
fi
}
getColumnData()
{
echo `echo $1|cut -d "," -f1,2 --output-delimiter " "`
}
containsSegment()
{
if [[ $1 =~ "LSNCS808" ]] || [[ $1 =~ "LSNCS097" ]] || [[ $1 =~ "LSNCS148" ]]
then
echo 0;
else
echo 1;
fi
}
FOLDER=.
CURRENT_TIMESTAMP=$(date '+%Y-%m-%d-%H%M%S')
EXTENSION=DAT
SEGMENT_FILE=${FOLDER}/SEGMENT-${CURRENT_TIMESTAMP}.${EXTENSION}
NON_SEGMENT_FILE=${FOLDER}/NONSEGMENT-${CURRENT_TIMESTAMP}.${EXTENSION}
# Assuming the first column in the record is always numeric and it is the header of the record
# any line not following the above pattern will be treated as the data belong to the header record
# Skipping the first line in the file
echo " Processing the file $1..."
sed 1d $1 |while read line
do
firstCol=(`getColumnData $line`)
isRecordHeader=`isNumeric ${firstCol[0]}`;
if [[ $isRecordHeader =~ "0" ]]
then
if [[ `containsSegment ${firstCol[1]}` =~ "0" ]]
then
#The data has to be appended to the segment file
filetoAppend=$SEGMENT_FILE;
else
#the data has to be appended to the non segment file
filetoAppend=$NON_SEGMENT_FILE;
fi
echo $line >>"$filetoAppend"
else
echo $line >>"$filetoAppend"
fi
done
echo " Finished processing the file $1..."
echo " Generated the following files: $SEGMENT_FILE, $NON_SEGMENT_FILE"
I have the above script with me..
Now the change i am looking for is I need the column names to be added in SEGMENT_FILE and NON SEGMENT_FILE..
I have the below solution but dont know exactly to place the below lines
You need to just create both the files at the beginning of the execution
touch $SEGMENT_FILE
touch $NON_SEGMENT_FILE
Then append the first line from the csv file
sed '1!d' $1 >> $SEGMENT_FILE
sed '1!d' $1 >> $NON_SEGMENT_FILE