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
# 8  
Old 07-08-2013
Another awk approach:
Code:
awk -F, -v P=10 -v R="DESCRIPTION" -v M="CURRENT_SALES" '
        NR == 1 {
                for ( i = 1; i <= NF; i++ )
                {
                        if ( $i == R )
                                d_pos = i
                        else if ( $i == M )
                                c_pos = i
                        else if ( i == P + 2 )
                                printf ( i == NF ? $c_pos OFS $i RS : $c_pos OFS $i OFS )
                        else
                                printf ( i == NF ? $i RS : $i OFS )
                }
        }
        NR > 1 {
                for ( i = 1; i <= NF; i++ )
                {
                        if ( i != d_pos && i != c_pos && i != P + 2 )
                                printf ( i == NF ? $i RS : $i OFS )
                        if ( i == P + 2 )
                                printf ( i == NF ? $c_pos OFS $i RS : $c_pos OFS $i OFS )
                }

        }
' OFS=, file

This User Gave Thanks to Yoda For This Post:
# 9  
Old 07-08-2013
I think taht was just a matter of the right/wrong field separator. Try
Code:
awk     'NR==1          {for (i=1; i<=NF; i++)  {if ($i == rem) RM=i
                                                 if ($i == rep) PL=i
                                                }
                         RCOL=10 + (RM<10) + (PL<10)
                        }
                        {for (i=1; i<=NF; i++)  {if (i == RCOL) printf ("%s%s", $PL, OFS)
                                                 if (i != RM && i != PL) printf ("%s%s", $i, OFS)
                                                }
                        print ""
                        }
        ' FS="," OFS="," rem="DESCRIPTION" rep="CURRENT_SALES" file
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,


@Yoda: What if the CURRENT_SALES position is AFTER col 10?
This User Gave Thanks to RudiC For This Post:
# 10  
Old 07-08-2013
Quote:
Originally Posted by RudiC
@Yoda: What if the CURRENT_SALES position is AFTER col 10?
That would be a problem. I assumed it would be always before field: 10. BTW there are trailing comma in your output, not sure if that is OK for the OP.
# 11  
Old 07-09-2013
@Yoda,@RudiC: Thanks for your solutions..

I am getting expected output from your solutions.
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