I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it is.
echo '1,Test Name,"This is a test and is funny",,,1234' | sed -e 's/"//g' -e 's/\([^,]\{0,10\}\)[^,]*,\([^,]\{0,8\}\)[^,]*,\([^,]\{0,21\}\)[^,]*,\([^,]\{0,5\}\)[^,]*,\([^,]\{0,5\}\)[^,]*,\([^,]\{0,5\}\)[^,]*/\1,\2,\3,\4,\5,\6/'
sed -e 's/"//g' -e 's/\([^,]\{0,10\}\)[^,]*,\([^,]\{0,8\}\)[^,]*,\([^,]\{0,21\}\)[^,]*,\([^,]\{0,5\}\)[^,]*,\([^,]\{0,5\}\)[^,]*,\([^,]\{0,5\}\)[^,]*/\1,\2,\3,\4,\5,\6/' <file>
#!/usr/bin/python
for line in open("csvfile"):
line = line.strip().split(',')
print "%s,%s,%s,%s,%s,%s" % (line[0][0:10] ,line[1][0:8],line[2].strip('"')[0:21],line[3][0:5],line[4][0:5],line[5][0:5])
Thank you so much! But one last question probably...
my script looks like this....
#!/bin/ksh
echo "started at " $(date);
while read record
do
echo $record | sed -e 's/"//g' -e 's/\([^,]\{0,12\}\)[^,]*,\([^,]\{0,35\}\)[^,]*,\([^,]\{0,35\}\)[^,]*,\([^,]\{0,35\}\)[^,
]*,\([^,]\{0,20\}\)[^,]*,\([^,]\{0,10\}\)[^,]*,\([^,]\{0,13\}\)[^,]*,\([^,]\{0,5\}\)[^,]*,\([^,]\{0,35\}\)[^,]*,\([^,]\{0,35\
}\)[^,]*,\([^,]\{0,13\}\)[^,]*,\([^,]\{0,5\}\)[^,]*,\([^,]\{0,31\}\)[^,]*,\([^,]\{0,75\}\)[^,]*,\([^,]\{0,180\}\)[^,]*/\1,\2,
\3,\4,\5,\6,\7,\8,\9,\"10",\"11",\"12",\"13",\"14",\"15"/' >> test_data_2.dat
done < test_email
echo "ended at " $(date)
exit;
and data in tets_email file is ...
97 Metro Packaging 160 Fornelius Ave Clifton NJ 7013 (973) 709-9100 289 Jack Bhohj Steven Neal (973) 709-9100 218 Call for appt between 0600 and 1400 M-F. Leave MSG if get voicemail. 973-777-3999 Warehouse direct line for emergencies POC William Toro. Shipping 0800 to 1600 M to F only.
98 Anchor Glass & Container 151 East McCanns Blvd. Elmira NY 14903 (607) 737-1933 324 Bill Weston Mike Sopp (607) 737-1933 300 Shipping hours 0800 to 2200 M to F.
278 Tate & Lyle #0278 Rt. 4 950 Morning Star Rd. Houlton ME 4730 (207) 532-9523 Load Hours: 7AM-2:00PM (EST) Requires Appt.
509 QUINCY PLANT 4551 SQUIRES ROAD QUINCY MI 49082 (517) 689-2391 Ed Loftis Charlotte Laws (517) 689-2391 edward.loftis@conagrafoods.com#http://edward.loftis@conagrafoods.com# Charlotte Laws (2nd Shift Lead Person) charlotte.laws@conagrafoods.com (517-689-2391)
786 Tate & Lyle / Specialty Warehouse #0786 333 Blair Bend Dr. Loudon TN 37774 (865) 458-9585 Load Hours: 8AM-3:00PM (EST) First come, first serve.
2243 Tate & Lyle / Distribution Center #2243 4464 E. 350 South Lafayette IN 47905 (765) 474-2512 Shipping Load Hours: 7AM-6:30PM (EST) First come, first serve.
2247 Tate & Lyle / McLeod Warehouse #2247 4988 Cundiff Circle Decatur IL 62526 (217) 877-9626
When I execute it, I get "sed garbled error".. plz help!!!