How to avoid Delimiter occuring in column values in .csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to avoid Delimiter occuring in column values in .csv file
# 1  
Old 06-25-2012
How to avoid Delimiter occuring in column values in .csv file

Hello Gurus,

I need to create a file from a .csv file extracting specific columns only.

File structure is
Code:
Column1,Column2,Column3,Column4
abcd,1234,"asdf, tew,123",123456
efgh,234,asdf,654321

My output file should have
Code:
abcd,123456
efgh,654321

Can you pls help me with the code.

Thanks

Moderator's Comments:
Mod Comment Please use code tags, thanks!

Last edited by zaxxon; 06-25-2012 at 08:50 AM..
# 2  
Old 06-25-2012
What you have tried so far ..
# 3  
Old 06-25-2012
For this case particulary :

Code:
awk -F"," '{print $1","$NF}' inputFile

. Will not work if the last column has more than one field wrapped inside quotes.
# 4  
Old 06-25-2012
I tried cut -d but it takes the commas inside "" as well in count
Code:
while read line
do
   OUTLINE=`echo $line | cut -d, -f1`","`echo $line | cut -d, -f4`
   echo $OUTLINE
done < file.csv

I get following output :
Code:
Column1,Column4
abcd, tew

Thanks

Last edited by zaxxon; 06-25-2012 at 08:50 AM.. Reason: Please use code tags for data and code samples
# 5  
Old 06-25-2012
The answer from @Sheel looks good because it finds the first and last comma-separated field (providing that both fields are numbers or strings which contain no comma characters).
# 6  
Old 06-25-2012
Quote:
Originally Posted by Sheel
For this case particulary :

Code:
awk -F"," '{print $1","$NF}' inputFile

. Will not work if the last column has more than one field wrapped inside quotes.
Thanks Sheel... But it's possible that I have more that one fields wrapped inside in the last column as well, I need to somehow escape commas inside "". Really appreciate if you can provide an alternative.
# 7  
Old 06-25-2012
I dont think we can do it in a single line. I can suggest you a way though: Filter out the rows having double quotes into a new file and write a command particualry for that file.

---------- Post updated at 07:22 AM ---------- Previous update was at 06:49 AM ----------

Here is another way. Replace the commas withing double quotes with a different character (say pipe) and then read the columns using comma as delimiter.

Command to replace the commas within double quotes
Code:
 awk -F'"' 'BEGIN{OFS="\""}{gsub(",","|",$2);print}' inputFile

You can revert the pipe to comma after getting the desired output.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Identify duplicate values at first column in csv file

Input 1,ABCD,no 2,system,yes 3,ABCD,yes 4,XYZ,no 5,XYZ,yes 6,pc,noCode used to find duplicate with regard to 2nd column awk 'NR == 1 {p=$2; next} p == $2 { print "Line" NR "$2 is duplicated"} {p=$2}' FS="," ./input.csv Now is there a wise way to de-duplicate the entire line (remove... (4 Replies)
Discussion started by: deadyetagain
4 Replies

2. Linux

To get all the columns in a CSV file based on unique values of particular column

cat sample.csv ID,Name,no 1,AAA,1 2,BBB,1 3,AAA,1 4,BBB,1 cut -d',' -f2 sample.csv | sort | uniq this gives only the 2nd column values Name AAA BBB How to I get all the columns of CSV along with this? (1 Reply)
Discussion started by: sanvel
1 Replies

3. Shell Programming and Scripting

Remove the values from a certain column without deleting the Column name in a .CSV file

(14 Replies)
Discussion started by: dhruuv369
14 Replies

4. Shell Programming and Scripting

Fetching values in CSV file based on column name

input.csv: Field1,Field2,Field3,Field4,Field4 abc ,123 ,xyz ,000 ,pqr mno ,123 ,dfr ,111 ,bbb output: Field2,Field4 123 ,000 123 ,111 how to fetch the values of Field4 where Field2='123' I don't want to fetch the values based on column position. Instead want to... (10 Replies)
Discussion started by: bharathbangalor
10 Replies

5. Linux

Filter a .CSV file based on the 5th column values

I have a .CSV file with the below format: "column 1","column 2","column 3","column 4","column 5","column 6","column 7","column 8","column 9","column 10 "12310","42324564756","a simple string with a , comma","string with or, without commas","string 1","USD","12","70%","08/01/2013",""... (2 Replies)
Discussion started by: dhruuv369
2 Replies

6. Shell Programming and Scripting

Script for extracting data from csv file based on column values.

Hi all, I am new to shell script.I need your help to write a shell script. I need to write a shell script to extract data from a .csv file where columns are ',' separated. The file has 5 columns having values say column 1,column 2.....column 5 as below along with their valuesm.... (3 Replies)
Discussion started by: Vivekit82
3 Replies

7. Shell Programming and Scripting

How to delete a column/columns of a CSV file which has cell values with a string enclosed in " , "?

How can I delete a column from a CSV file which has comma separated value with a string enclosed in double quotes and a comma in between? I have a file 44.csv with 4 lines including the header like the below format: column1, column2, column3, column 4, column5, column6 12,455,"string with... (6 Replies)
Discussion started by: dhruuv369
6 Replies

8. Shell Programming and Scripting

Check to identify duplicate values at first column in csv file

Hello experts, I have a requirement where I have to implement two checks on a csv file: 1. Check to see if the value in first column is duplicate, if any value is duplicate script should exit. 2. Check to verify if the value at second column is between "yes" or "no", if it is anything else... (4 Replies)
Discussion started by: avikaljain
4 Replies

9. Shell Programming and Scripting

return a list of unique values of a column from csv format file

Hi all, I have a huge csv file with the following format of data, Num SNPs, 549997 Total SNPs,555352 Num Samples, 157 SNP, SampleID, Allele1, Allele2 A001,AB1,A,A A002,AB1,A,A A003,AB1,A,A ... ... ... I would like to write out a list of unique SNP (column 1). Could you... (3 Replies)
Discussion started by: phoeberunner
3 Replies
Login or Register to Ask a Question