Delimited files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delimited files
# 1  
Old 01-10-2006
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  
Old 01-10-2006
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  
Old 01-10-2006
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  
Old 01-10-2006
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  
Old 02-01-2006
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Match tab-delimited files based on key

I thought I had this figured out but was wrong so am humbly asking for help. The task is to add an additional column to FILE 1 based on records in FILE 2. The key is in COLUMN 1 for FILE 1 and in COLUMN 1 OR COLUMN 2 for FILE 2. I want to add the third column from FILE 2 to the beginning of... (8 Replies)
Discussion started by: andmal
8 Replies

2. Shell Programming and Scripting

Help/Advise please for converting space delimited string variable to comma delimited with quote

Hi, I am wanting to create a script that will construct a SQL statement based on a a space delimited string that it read from a config file. Example of the SQL will be For example, it will read a string like "AAA BBB CCC" and assign to a variable named IN_STRING. I then concatenate... (2 Replies)
Discussion started by: newbie_01
2 Replies

3. UNIX for Dummies Questions & Answers

Need to convert a pipe delimited text file to tab delimited

Hi, I have a rquirement in unix as below . I have a text file with me seperated by | symbol and i need to generate a excel file through unix commands/script so that each value will go to each column. ex: Input Text file: 1|A|apple 2|B|bottle excel file to be generated as output as... (9 Replies)
Discussion started by: raja kakitapall
9 Replies

4. Shell Programming and Scripting

How to make tab delimited file to space delimited?

Hi How to make tab delimited file to space delimited? in put file: ABC kgy jkh ghj ash kjl o/p file: ABC kgy jkh ghj ash kjl Use code tags, thanks. (1 Reply)
Discussion started by: jagdishrout
1 Replies

5. Shell Programming and Scripting

Need a script to convert comma delimited files to semi colon delimited

Hi All, I need a unix script to convert .csv files to .skv files (changing a comma delimited file to a semi colon delimited file). I am a unix newbie and so don't know where to start. The script will be scheduled using cron and needs to convert each .csv file in a particular folder to a .skv... (4 Replies)
Discussion started by: CarpKing
4 Replies

6. Shell Programming and Scripting

Help with converting Pipe delimited file to Tab Delimited

I have a file which was pipe delimited, I need to make it tab delimited. I tried with sed but no use cat file | sed 's/|//t/g' The above command substituted "/t" not tab in the place of pipe. Sample file: abc|123|2012-01-30|2012-04-28|xyz have to convert to: abc 123... (6 Replies)
Discussion started by: karumudi7
6 Replies

7. UNIX for Dummies Questions & Answers

Three space delimited files

So I have three space delimited files. Each have 90 rows and many columns. I want to merge them by columns. I use paste to merge file 1 and file2 or file 3 and it works. But when I try to merge file 2 and file 3 using paste or merge or cat, I cannot merge them by column, instead they are merged by... (11 Replies)
Discussion started by: evelibertine
11 Replies

8. UNIX for Dummies Questions & Answers

Combining three space delimited files by column

I have three space delimited files, how do I combine them by column in Unix? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

9. Shell Programming and Scripting

Working with Tab-Delimited files

I have a tab-Delimited file: Eg: 'test' file contains: a<tab>b<tab>c<tab>.... Based on certain condition, I wanna increase the number of lines of this file.How do I do that Eg: If some value in the database is 1 then one line in 'test' file is fine.. If some value in the database is 2... (1 Reply)
Discussion started by: shiroh_1982
1 Replies

10. Shell Programming and Scripting

Removing trailing spaces from delimited files

Hi All I have a file of the following format (delimited by |) this is field 1 | field 2 (lots of blank spaces) | field 3 (lots of blank space) | field 1 | more text (lots of blank spaces) | dhjdsk | Is there a way I can remove... (6 Replies)
Discussion started by: djkane
6 Replies
Login or Register to Ask a Question