Visit Our UNIX and Linux User Community


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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove the values from a certain column without deleting the Column name in a .CSV file
# 1  
Old 12-18-2013
Remove the values from a certain column without deleting the Column name in a .CSV file

# 2  
Old 12-18-2013
Please provide and example of input you have as well as an example of output you expect.
In a way or another, the separator used between fields must not be used in fields' values ... or you will have to convert them to make the field separator reliable enough to use them to process your file formatting.
# 3  
Old 12-18-2013
Hello,

Could you please provide the input and expected ouput for same please.



Thanks,
R. Singh
# 4  
Old 12-18-2013
Hi, example with sed:
Code:
$ cat fich.csv
name1,name2,name3,name4,name5,name6,name7,name8,name9,name10,
val1,val2,val3,val4,val5,val6,val7,val8,val9,val10,
valx1,valx2,valx3,valx4,valx5,valx6,valx7,valx8,valx9,valx10,
valy1,valy2,valy3,valy4,valy5,valy6,valy7,valy8,valy9,valy10,
valz1,valz2,valz3,valz4,valz5,valz6,valz7,valz8,valz9,valz10,

Code:
$ sed '1!s/\(\([^,]*,\)\{4\}\)[^,]*/\1/' fich.csv
name1,name2,name3,name4,name5,name6,name7,name8,name9,name10,
val1,val2,val3,val4,,val6,val7,val8,val9,val10,
valx1,valx2,valx3,valx4,,valx6,valx7,valx8,valx9,valx10,
valy1,valy2,valy3,valy4,,valy6,valy7,valy8,valy9,valy10,
valz1,valz2,valz3,valz4,,valz6,valz7,valz8,valz9,valz10,

Regards.
# 5  
Old 12-18-2013
Sample would be like this:
Code:
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,
23455,12312255564,"string, with, multiple, commas,string with or, without commas",string 2,USD,433,70%,07/15/2013,
23525,74535243123,"string , with commas, and - hypens and: semicolans","string with or, without commas",string 1,CAND,744,70%,05/06/2013,
46476,15467534544,"lengthy string, with commas, multiple: colans","string with or, without commas",string 2,CAND,388,70%,09/21/2013,

Output expected:
Code:
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",,USD,12,70%,08/01/2013,
23455,12312255564,"string, with, multiple, commas,string with or, without commas",,USD,433,70%,07/15/2013,
23525,74535243123,"string , with commas, and - hypens and: semicolans","string with or, without commas",,CAND,744,70%,05/06/2013,
46476,15467534544,"lengthy string, with commas, multiple: colans","string with or, without commas",,CAND,388,70%,09/21/2013,


Last edited by dhruuv369; 12-18-2013 at 02:34 PM.. Reason: Sample file edited.
# 6  
Old 12-18-2013
Using awk

Code:
$ cat file
name1,name2,name3,name4,name5,name6,name7,name8,name9,name10
val1,val2,val3,val4,val5,val6,val7,val8,val9,val10
valx1,valx2,valx3,valx4,valx5,valx6,valx7,valx8,valx9,valx10
valy1,valy2,valy3,valy4,valy5,valy6,valy7,valy8,valy9,valy10
valz1,valz2,valz3,valz4,valz5,valz6,valz7,valz8,valz9,valz10

Code:
$ awk 'NR>1{$4=""}1' FS=, OFS=, file
name1,name2,name3,name4,name5,name6,name7,name8,name9,name10
val1,val2,val3,,val5,val6,val7,val8,val9,val10
valx1,valx2,valx3,,valx5,valx6,valx7,valx8,valx9,valx10
valy1,valy2,valy3,,valy5,valy6,valy7,valy8,valy9,valy10
valz1,valz2,valz3,,valz5,valz6,valz7,valz8,valz9,valz10

# 7  
Old 12-18-2013
@akash: I am not getting the output that I was expecting. Using your code, I got the output as below:

Code:
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 ,,"string with or, without commas",string 1,USD,12,70%,08/01/2013,
23455,12312255564,"string,, multiple, commas,string with or, without commas,string 2,USD,433,70%,07/15/2013,
23525,74535243123,"string ,, and - hypens and: semicolans","string with or, without commas",string 1,CAND,744,70%,05/06/2013,
46476,15467534544,"lengthy string,, multiple: colans","string with or, without commas",string 2,CAND,388,70%,09/21/2013,

Any other suggestions?

Previous Thread | Next Thread
Test Your Knowledge in Computers #869
Difficulty: Medium
Fortran introduced the concept of automatic garbage collection.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove duplicate values in a column(not in the file)

Hi Gurus, I have a file(weblog) as below abc|xyz|123|agentcode=sample code abcdeeess,agentcode=sample code abcdeeess,agentcode=sample code abcdeeess|agentadd=abcd stereet 23343,agentadd=abcd stereet 23343 sss|wwq|999|agentcode=sample1 code wqwdeeess,gentcode=sample1 code... (4 Replies)
Discussion started by: ratheeshjulk
4 Replies

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

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

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

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

7. Shell Programming and Scripting

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 Column1,Column2,Column3,Column4 abcd,1234,"asdf, tew,123",123456 efgh,234,asdf,654321 My output file should have abcd,123456 efgh,654321 Can you pls help me with the code. ... (10 Replies)
Discussion started by: ritesh.bhawsar
10 Replies

8. Shell Programming and Scripting

Remove line feed from csv file column

Hi All, i have a csv file . In the 7th column i have data that has line feed in it. Requirement is to remove the line feed from the 7th column whenever it appears There are 11 columns in the file C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11 The value in C7 contains line feed ( Alt + Enter ),... (2 Replies)
Discussion started by: r_t_1601
2 Replies

9. Shell Programming and Scripting

Remove line feed from csv file column

Hi All, My requirement is to remove line (3 Replies)
Discussion started by: r_t_1601
3 Replies

Featured Tech Videos