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
Delimited File Help garpal UNIX for Dummies Questions & Answers 6 03-28-2008 09:58 AM
awk - delimited output videsh77 UNIX for Advanced & Expert Users 1 12-04-2007 10:41 PM
Working with Tab-Delimited files shiroh_1982 Shell Programming and Scripting 1 05-31-2006 02:31 PM
Removing trailing spaces from delimited files djkane Shell Programming and Scripting 6 03-29-2006 06:11 AM
Converting Tab delimited file to Comma delimited file in Unix charan81 Shell Programming and Scripting 22 01-20-2006 09:24 AM

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 01-10-2006
hrshah74 hrshah74 is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 3
Delimited files

One of my files has record in following format.
"FSNAME_01102006B_input.csv",10,"First Name, Last Name"," CUST"

How can I split this line and store values in 4 different variables?

Thanks
  #2 (permalink)  
Old 01-10-2006
reborg's Avatar
reborg reborg is online now Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,190
Code:
#! /usr/bin/ksh

OLD_IFS="$IFS"
IFS=","

while read field1 field2 field3 field4 ; do
    echo "field1=$field1 field2=$field2 field3=$field3 field4=$field4"
done < file

IFS="$OLD_IFS"
  #3 (permalink)  
Old 01-10-2006
hrshah74 hrshah74 is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 3
Comma in field 3 is an issue

Hi
The comma in field 3 screws up your solution.
When I ran the script, I got following

field1="F000GB_01102006A_input.csv" field2=10 field3="Long Beach field4=
Acceptance Corp.","F000GB"

I want following format
field1="F000GB_01102006A_input.csv" field2=10 field3="Long Beach,
Acceptance Corp." field4="F000GB"
  #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
  #5 (permalink)  
Old 02-01-2006
hrshah74 hrshah74 is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 3
Almost similar format

Hi zazzybob,
It works for simple fomat that i attached in my post previously. But if doesn't work for following format.

"FSNAME_01102006B_input.csv",10, "Firstr, Name, Last, Name ", " CUSTr name "

Difference from the first format is...extra spaces after second and third comma. Is it possible to retrieve field values for these record?

Regards
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 08:13 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