Help with shell script

#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
1 Like

Is this homework?

no..I need solution a bit urgently

"Urgent" is a difficult word in these forums.

Not sure if I understand your requirement correctly, but you might want to place

sed 1q $1 | tee $NON_SEGMENT_FILE >$SEGMENT_FILE

right before your existing sed command. Drop the touch es and the sed '1!d' s

1 Like

The above suggestion worked perfectly all right..

I have an other requirement.
With the above script I will have 2 csv files $SEGMENT_FILE and $NON_SEGMENT_FILE.
Now with the $SEGMENT_FILE which is a csv file,I need to generate two more csv files based on the value in column 6 i.e F.
If the value in column 6 is 'VS KSDS' move the values to csv file VSAM_SEQUENTIAL and place the csv file at path /VSAM
Else if the value in column 6 is space move the values to csv file SEQ_SEQUENTIAL and place the csv file at path /SEQ

Please help me with a script for this

Flow control might branch you to the following code if you don't show what have you tried on this of your last requirement.

if [[ -z "$script" || -z "$effort" ]]; then
    echo "help" 2>&1 > /dev/null
    echo "urgent" 2>&1 > /dev/null
fi

Sorry i dint get wat you are try to telling

As here are multiple rule violations, I'll close this down now... Please read the rules again and act accordingly.