Extract a nth field from a comma delimited file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract a nth field from a comma delimited file
# 1  
Old 01-19-2012
Extract a nth field from a comma delimited file

Hi,
In my file (which is "," delimited and text qualifier is "), I have to extract a particualr field.
Code:
file1:
1,"aa,b",4
 
expected is the 2nd field:
aa,b
 
I tried the basic cut -d "," -f 2 file 1, this gave me
aa alone instead aa,b.

A small hint ot help on this will be very helpful.
# 2  
Old 01-19-2012
This isn't really a comma-delimited file if you've got commas inside some of your fields. Having extra delimiters inside your fields is a pretty bad idea for this reason. If you must have delimiters in your fields, having only some of your fields inside quotes makes it even harder. None of the usual tools are going to be able to handle this file in an elegant way.

---------- Post updated at 11:02 AM ---------- Previous update was at 10:24 AM ----------

Code:
$ cat subquote.awk
{
        POS=1
        STR=""

        while(POS <= length($0))
        {
                SUB=substr($0, POS);
                OFF=match(SUB, /"[^"]*"/);

                if(OFF)
                {
                        BEF=substr(SUB, 1, RSTART-1);
                        M=substr(SUB, RSTART, RLENGTH);
                        AFTER=substr(SUB, RSTART+RLENGTH);

                        gsub(/,/, "\\,", M);

                        STR=STR BEF M;
                        POS+=(RSTART+RLENGTH)-1;
                }
                else
                {
                        STR=STR SUB;
                        POS=length($0)+1;
                }
        }

        print STR;
}

$ cat quotes.sh

#!/bin/sh

        # Convert "a,b" into "a\,b"
awk -f subquotes.awk datafile |
        # Read will take \, to be a literal , and others as separators
while IFS="," read A B C
do
        echo $B
done

$ ./quotes.sh

"aa,bb"

$

You may need to use nawk or gawk on some systems.
# 3  
Old 01-19-2012

If the field in question will always be quoted, use quotation marks as the delimiter:
Code:
field=2
cut -d\" -f "$field" "$file"

# 4  
Old 01-20-2012
You could first transform the file into a ; delimited file, for example and remove the double quotes. Then you could extract the field in a separate awk:
Code:
$ echo '1,"aa,b",4' | awk 'NR%2{gsub(/,/,";")}1' RS=\" ORS= 
1;aa,b;4

Code:
$ echo '1,"aa,b",4' | awk 'NR%2{gsub(/,/,";")}1' RS=\" ORS= | awk -F\; '{print $2}'
aa,b

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 01-20-2012
Scru's code works fine.
Can you please explain the code?
# 6  
Old 01-20-2012
OK, I presume you mean the first awk.
NR%2On every odd record. Because a double quote is used as a record separator, every odd record is outside the double quotes and every even record is within
{gsub(/,/,";")}Replace every comma with a semicolon
1perform the default action i.e. print every record (equivalent to {print $0})
RS=\" Use a double quote as a record separator.
ORS= Use nothing as an output record separator, thus removing the double quotes from the output
I hope this explains it..

S.
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

How can i comma-delimited last field in line?

Awk gurus, Greatly appreciate for any kind of assistance from the expert community Input line: abc,11.22.33.44,xyz,7-8-9-10 pqr,111.222.333.444,wxy,1-2-3 def,22.33.44.55,stu,7-8 used the gsub function below but it changes all of the "-" delimiter: awk 'gsub("-",",")' Desired... (4 Replies)
Discussion started by: ux4me
4 Replies

3. Shell Programming and Scripting

How to extract field delimited by comma and store into an array?

hi, i have a variable which contains some file names separated by comma. example FNAME="abc.txt,def.txt,ghi.txt" i want to extract each filename and store it into an array and also count the number of files in the array. file=abc.txt file=def.txt file=ghi.txt i thought of using the... (8 Replies)
Discussion started by: Little
8 Replies

4. 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

5. Shell Programming and Scripting

How to read nth word from delimited text file?

Hi i am new in scripting how i can get 2 elements from first line of delimited txt file in shell scripts. AA~101010~0~AB~8000~ABC0~ BB~101011~0~BC~8000~ABC~ CC~101012~0~CD~8000~ABC0~ DD~101013~0~AB~8000~ABC~ AA~101014~0~BC~8000~ABC0~ CC~101015~0~CD~8000~ABC~ can anyone plse help?... (3 Replies)
Discussion started by: sushine11
3 Replies

6. UNIX for Dummies Questions & Answers

Flat File - Comma Delimited

I have a flat file whose contents are comma delimited and there are 84 columns in total, so everytime I try to view the contents, things get over lapped it becomes diffcult to read through the result set. Is there a command / what would be the best way...if I want to view the results alligned... (4 Replies)
Discussion started by: priya33184
4 Replies

7. UNIX for Dummies Questions & Answers

nth Columns in a Tab delimited file

Hi Can anyone help me to identify the nth field from a Tab delimited file? Thanks Subrat (8 Replies)
Discussion started by: subrat
8 Replies

8. UNIX for Dummies Questions & Answers

Comma delimited file

Hi All, I have output of sql saved in comma separated file. Now i need to read line by line this file and assign word to a unix variable for further processing Eg: Test file world, 1, 3, 4 earth,2,3,4,5 moon,1,2,3,4 Output should be word1= world word2=1 echo " first word... (7 Replies)
Discussion started by: gwrm
7 Replies

9. Shell Programming and Scripting

Converting Tab delimited file to Comma delimited file in Unix

Hi, Can anyone let me know on how to convert a Tab delimited file to Comma delimited file in Unix Thanks!! (22 Replies)
Discussion started by: charan81
22 Replies

10. Shell Programming and Scripting

Comma Delimited file

I have a comma delimited file that sometimes has addresses details in. The problem is that the address detail can be seen as: "Sample House, Sample Road". When I run a script specifying the file is comma delimited I would like it to ignore comma's that are in between speech marks. Is this... (2 Replies)
Discussion started by: dbrundrett
2 Replies
Login or Register to Ask a Question