The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
shell scripting vijray27 Shell Programming and Scripting 4 02-27-2009 09:44 AM
Shell scripting jack00423 Shell Programming and Scripting 1 09-30-2008 12:48 AM
difference between AIX shell scripting and Unix shell scripting. haroonec Shell Programming and Scripting 2 04-12-2006 08:12 AM
Shell scripting dipanrc Shell Programming and Scripting 4 10-27-2005 03:53 AM
something else on shell scripting master_6ez Shell Programming and Scripting 1 11-21-2004 11:42 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-19-2009
chandrakala chandrakala is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 13
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
  #2 (permalink)  
Old 03-19-2009
dariyoosh's Avatar
dariyoosh dariyoosh is offline
Registered User
  
 

Join Date: Mar 2009
Location: Iran (Tehran)
Posts: 44
Hello there,

I think that the following KornShell script does the job

Code:
#!/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,
  #3 (permalink)  
Old 03-19-2009
sunpraveen's Avatar
sunpraveen sunpraveen is offline
Registered User
  
 

Join Date: Jan 2009
Location: Pruthvi
Posts: 85
Try this:

Code:
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,

Regards,

Praveen
  #4 (permalink)  
Old 03-19-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,415
Code:
# 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
  #5 (permalink)  
Old 03-19-2009
chandrakala chandrakala is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 13
Its working dariyoosh.

Many thanks.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 09:42 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0