need a help in shell scripting

Hi Folks,
I need a help in shell scripting.

I have a CSV file which got 1000 records in it..here it goes

KRW71,GAMDE1,D,14656,17909.5,20090217,48324,20090202,22060,K,23897,,,,2,Truck,27.2,,,O,T
KRW71,GAMDE1,D,14656,17909.5,20090217,48325,20090203,22060,K,10430,,,,1,Truck,13.6,,,O,T
KRW71,GAMDE1,D,14656,17909.5,20090217,48326,20090204,22060,K,20376,,,,2,Truck,27.2,,,O,T
KRW71,GAMDE1,D,14656,17909.5,20090217,48327,20090205,22060,K,19338,,,,1,Truck,13.6,,,O,T
KRW71,GAMDE1,D,14656,17909.5,20090217,48328,20090206,22060,K,20682,,,,1,Truck,13.6,,,O,T

I want to add a tag FBINV at the beginning of each line if its not present already.

Can you please help me out in writing a script for above one.

Any help would be apptreciated.

Thanks in advance

Hello there,

I think that the following KornShell script does the job

#!/bin/ksh

RESULT=""
TARGET_TOKEN="FBINV,"

while read LINE
do
    if [[ $LINE = TARGET_TOKEN* ]]
    then
        RESULT="$RESULT$LINE\n"
    else
        RESULT="$RESULT$TARGET_TOKEN$LINE\n"
    fi
done < $1

RESULT="$RESULT\n"
print "$RESULT" > $1

Regards,
:slight_smile:

Try this:

cat filename | while read line
do
  fbinv_cnt=$(echo ${line} | grep -i "FBINV" | wc -l)
  if [[ ${fbinv_cnt} -eq 0 ]]; then
    line=$(echo "FBINV,${line}")
  fi
  echo ${line} >> outputfile.csv
done

HTH, :cool:

Regards,

Praveen

# awk '!/^FBINV/{print "FBINV,"$0}' file
FBINV,KRW71,GAMDE1,D,14656,17909.5,20090217,48324,20090202,22060,K,23897,,,,2,Truck,27.2,,,O,T
FBINV,KRW71,GAMDE1,D,14656,17909.5,20090217,48325,20090203,22060,K,10430,,,,1,Truck,13.6,,,O,T
FBINV,KRW71,GAMDE1,D,14656,17909.5,20090217,48326,20090204,22060,K,20376,,,,2,Truck,27.2,,,O,T
FBINV,KRW71,GAMDE1,D,14656,17909.5,20090217,48327,20090205,22060,K,19338,,,,1,Truck,13.6,,,O,T
FBINV,KRW71,GAMDE1,D,14656,17909.5,20090217,48328,20090206,22060,K,20682,,,,1,Truck,13.6,,,O,T

Its working dariyoosh.

Many thanks.:b: