The UNIX and Linux Forums  


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




Thread: Delimited files
View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 01-10-2006
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
This is *very* specific to the input line you gave in your original post. It is not the most efficient solution, but it works using your supplied input.

Code:
[root@MYAUSLV00100118 tmp]# cat ./foo.ksh
#! /bin/bash

while read line; do
    field1=`echo "${line}" | sed 's/^\([^,]*\),.*$/\1/'`
    field2=`echo "${line}" | sed 's/^[^,]*,\([^,]*\),.*$/\1/'`
    field3=`echo "${line}" | sed 's/^[^,]*,[^,]*,\(\"[^\"]*\"\),.*$/\1/'`
    field4=`echo "${line}" | sed 's/^[^,]*,[^,]*,\"[^\"]*\",\([^,]*\).*$/\1/'`
    echo "field1=${field1}"
    echo "field2=${field2}"
    echo "field3=${field3}"
    echo "field4=${field4}"
done < foo.csv

exit 0
[root@MYAUSLV00100118 tmp]# cat ./foo.csv
"FSNAME_01102006B_input.csv",10,"First Name, Last Name"," CUST"
[root@MYAUSLV00100118 tmp]# ./foo.ksh
field1="FSNAME_01102006B_input.csv"
field2=10
field3="First Name, Last Name"
field4=" CUST"
Cheers
ZB