how to cut off last column from the output

I have a problem with my script. I am using following code
awk -F"," '{print $0,",",substr($2,3,3)}' $REG_InputFileName > $TargetSeqPath/Master.tmp

while read i
do
echo $i > $TargetSeqPath/Ref.tmp
OutFileName=`awk -F"," '{print $3}' $TargetSeqPath/Ref.tmp`
rm -f $TargetSeqPath/$OutFileName.dat
touch $TargetSeqPath/$OutFileName.dat
awk -F"," 'NR==FNR {col1[$1]=$3} NR!=FNR { if (col1[substr($2,3,3)]=="") {next} print $0}' $TargetSeqPath/Ref.tmp $TargetSeqPath/Master.tmp >> $TargetSeqPa
th/$OutFileName.dat
done < $RefInputFileName

In this script i am having output as following :

"UPDATE", "3000020113", "1-01-01", "THB" , 300
"UPDATE", "3000020307", "1-01-01", "THB" , 300
"UPDATE", "3000020409", "1-01-01", "USD" , 300
"UPDATE", "3000021126", "1-01-01", "USD" , 300

but i want to get rid of last column . Can anybody help me with this?

Thanks

Post your input file and the desired output.

Regards

REG_InputFileName :
"INSERT", "1000011206", "1-01-01", "THB"
"INSERT", "1000020307", "1-01-01", "USD"
"INSERT", "2000020113", "1-01-01", "USD"
"INSERT", "2000020411", "1-01-01", "THB"
"INSERT", "2000041125", "1-01-01", "USD"
"UPDATE", "1000020113", "1-01-01", "THB"
"UPDATE", "1000020307", "1-01-01", "THB"
"UPDATE", "1000020411", "1-01-01", "THB"
"UPDATE", "2000020411", "1-01-01", "USD"
"UPDATE", "2000020722", "1-01-01", "USD"
"UPDATE", "2000031221", "1-01-01", "THB"
"UPDATE", "3000020113", "1-01-01", "THB"
"UPDATE", "3000020307", "1-01-01", "THB"
"UPDATE", "3000020409", "1-01-01", "USD"
"UPDATE", "3000021126", "1-01-01", "USD"

RefInputFileName
100,OHC,MYH
200,OHC,SGH
300,OHC,INM

There will be three output files namely INM.dat, MYH.dat and SGH.dat
output for INM.dat is :

"UPDATE", "3000020113", "1-01-01", "THB"
"UPDATE", "3000020307", "1-01-01", "THB"
"UPDATE", "3000020409", "1-01-01", "USD"
"UPDATE", "3000021126", "1-01-01", "USD"

Use nawk, gawk or /usr/xpg4/bin/awk on Solaris.

awk -F, 'NR==FNR{a[$1]=$3;next}
substr($2,3,3) in a{print > a[substr($2,3,3)]".dat"}
' RefInputFileName REG_InputFileName

Regards

Hi Franklin,
I didn't understand where i need to use that code??

It should be something like:

#!/bin/sh
  
RefInputFileName=".."  # set your variable here
REG_InputFileName=".."  # set your variable here

awk -F, 'NR==FNR{a[$1]=$3;next}
substr($2,3,3) in a{print > a[substr($2,3,3)]".dat"}
' "$RefInputFileName" "$REG_InputFileName"

Regards

Hi franklin,

i am new to awk . Can you please exactly tell me where should i put this statement in my code. My code is exactly as above.

> echo "UPDATE", "3000020113", "1-01-01", "THB" , 300 | awk 'IFS="," {print $1,$2,$3,$4}'
UPDATE, 3000020113, 1-01-01, THB

> echo "UPDATE", "3000020113", "1-01-01", "THB" , 300 | awk -F, 'OFS="," {print $1,$2,$3,$4}'
UPDATE, 3000020113, 1-01-01, THB

Thanks...But this is kind of hard coded. Right now i only have sample input data to test my script. and i have no idea about the total number of fileds. So i can't use this code to filter...can you please provide something else which will only cut off last column from the output regardless of number of columns in it.

Thanks again :slight_smile:

Ok, forget your script for a while and post the contents of the original file (not temporary files produced by your script) and the desired output.

Regards