Delete Description column from a csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete Description column from a csv file
# 1  
Old 07-08-2013
Delete Description column from a csv file

Hi Friends,

I have a scenario where in I need to implement the following logics.

1. Delete a column named "DESCRIPTION'" from a csv file(sample.csv).
2. Move "CURRENT_SALES" column to 10th position in columns from the same file.


Thanks in Advance,
Pons
# 2  
Old 07-08-2013
Do you know the column number of DESCRIPTION? What is the current column number of CURRENT_SALES?
# 3  
Old 07-08-2013
These two columns can be at any location in the file.
# 4  
Old 07-08-2013
It is usually wise to post sample input and output files to avoid bad surprises with proposals. However, try this:
Code:
awk     'NR==1          {for (i=1; i<=NF; i++)  {if ($i == rem) RM=i
                                                 if ($i == rep) PL=i
                                                }
                        }
                        {for (i=1; i<=NF; i++)  {if (i == 10) printf ("%s%s", $PL, OFS)
                                                 if (i != RM && i != PL) printf ("%s%s", $i, OFS)
                                                }
                        print ""
                        }
        ' rem="DESCRIPTION" rep="CURRENT_SALES" file

# 5  
Old 07-08-2013
Hi Rudic, Thanks for your input.

Here I explain the requirement.
I want to delete the "DESCRIPTION" column a comma delimited file. I am not able to do this with cut command as the description column position is dynamic.
Also I want to move "CURRENT_SALES" column from 4 the position to 10th position column.

sample Input:
Code:
P_ID,P_NAME,DESCRIPTION,CURRENT_SALES,COL5,COL6,COL7,COL8,COL9,COL10,COL11,COL12
10,A,product A,$100,0,0,0,0,0,0,0,0
20,B,product B,$400,0,0,0,0,0,0,0,0
30,C,product C,$800,0,0,0,0,0,0,0,0

Output:
Code:
P_ID,P_NAME,COL5,COL6,COL7,COL8,COL9,COL10,COL11,CURRENT_SALES,COL12
10,A,0,0,0,0,0,0,0,$100,0
20,B,0,0,0,0,0,0,0,$400,0
30,C,0,0,0,0,0,0,0,$800,0

Thanks in advance.

Pons

Last edited by Franklin52; 07-08-2013 at 10:52 AM.. Reason: Please use code tags
# 6  
Old 07-08-2013
Well, did you try the proposal (after minor adaption)?
# 7  
Old 07-08-2013
I tried your first solution. and I am not getting expected output as not updating it correctly.
Please help me in fixing the issue.

Thanks
Pons
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare every column from one csv file to another csv file

1.csv contains following column- Empid code loc port 101 A xy 01 102 B zx 78 103 A cg 12 104 G xy 78 2.csv contains follwing data- Empid code loc port 101 A gf 01 102 B zx 78 103 C cg 32 104 ... (1 Reply)
Discussion started by: rishabh
1 Replies

2. Shell Programming and Scripting

Get maximum per column from CSV file, based on date column

Hello everyone, I am using ksh on Solaris 10 and I'm gathering data in a CSV file that looks like this: 20170628-23:25:01,1,0,0,1,1,1,1,55,55,1 20170628-23:30:01,1,0,0,1,1,1,1,56,56,1 20170628-23:35:00,1,0,0,1,1,2,1,57,57,2 20170628-23:40:00,1,0,0,1,1,1,1,58,58,2... (6 Replies)
Discussion started by: ejianu
6 Replies

3. Shell Programming and Scripting

Cryption and description for csv file

Hi, Requirement is encrypt key should pick value from file instead of passing their in command. how can i create .p file which contains crypt value. i.e. crypt <trade.csv> tr.x $CKEY fcrypt.p crypt < tr.x $CKEY fcrypt.p i want to encrypt and decrpt csv file and i am using following... (2 Replies)
Discussion started by: rizwan.shaukat
2 Replies

4. Shell Programming and Scripting

Bash - delete from csv all the row if the first column is length >

Hi guys, i have a csv file like: USERID;COG;DESCR;FIL;OFF user001;user;test1;001;A01 user002;user;test2;002;A02 user0003;user;test3;003;A03 user004;user;test4;004;A04 user0005;user;test5;005;A05 etc.. I need to read line for line and, if value of first column is > 7 char (in this example... (4 Replies)
Discussion started by: kamose
4 Replies

5. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

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

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

Pick the column value based on another column from .csv file

My scenario is that I need to pick value from third column based on fourth column value, if fourth column value is 1 then first value of third column.Third column (2|3|4|6|1) values are cancatenated. Main imp point, in my .csv file, third column is having price value with comma (1,20,300), it has... (2 Replies)
Discussion started by: Ganesh L
2 Replies

9. UNIX for Dummies Questions & Answers

Delete first line of a csv file

Hi there How do I delete the first line of a csv file without creating a new file ? Regards, Peter (4 Replies)
Discussion started by: nagileon
4 Replies
Login or Register to Ask a Question