Deleting specific columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting specific columns
# 8  
Old 09-11-2012
Quote:
Originally Posted by pamu
Code:
printf$i OFS

It is extremely bad practice to pass miscellaneous text where printf requires a format string. In awk, worst case is a runtime error. With C, it's a massive security hole. For more info, read Uncontrolled format string - Wikipedia, the free encyclopedia

For your sake, and for the sake of those who may read your posts and emulate your code, adopt a safe, reliable approach. Instead of printf $i OFS, use printf '%s', $i OFS.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 9  
Old 09-11-2012
Like this?
Code:
cols="3,5"
awk -F'\t' -v cols="$cols" 'BEGIN{n=split(cols,t,",")}{
for(i=1;i<=n;i++) $t[i]="";gsub(FS"+",FS);gsub("^"FS"+|"FS"+$","")}1' OFS='\t' file

Not very pretty and robust (esp. with a different FS), I know. May be someone will come up with a better/wittier solution. I hope you know to get the values in the variable cols from the user.

Last edited by elixir_sinari; 09-11-2012 at 10:35 AM..
# 10  
Old 09-11-2012
Quote:
Originally Posted by elixir_sinari
Like this?
Code:
cols="3,5"
awk -F'\t' -v cols="$cols" 'BEGIN{n=split(cols,t,",")}{
for(i=1;i<=n;i++) $t[i]="";gsub(FS"+",FS);gsub("^"FS"+|"FS"+$","")}1' OFS='\t' file

Not very pretty and robust (esp. with a different FS), I know. May be someone will come up with a better/wittier solution. I hope you know to get the values in the variable cols from the user.
That's worse than "not very pretty and robust." It cannot distinguish between empty fields in the data and fields that have been nulled during the "cutting" process. If the output should contain field x and x is null from the beginning, instead of the output containing a null field it will be missing the field.


The following is a better approach:
Code:
# Usage: awk -v cols=f1,f2,... -f scriptpath input-file

BEGIN {
        split(cols, a, /,/)
        for (i in a)
                cut[a[i]]
}

{
        p=0
        for (i=1; i<=NF; i++) {
                if (i in cut)
                        continue
                printf "%s%s", (p ? OFS : ""), $i
                p=1
        }
        print ""
}

Further logic could be added to the BEGIN clause, or the invoking shell script, to comprehend field ranges (1-3,7-9), but this code needs each column listed explictly and individually in a comma-delimited string (1,2,3,7,8,9). Repetition and empty fields in the cols string are not a problem.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 11  
Old 09-11-2012
OK..OK..I missed that.
Thanks for pointing out..
# 12  
Old 09-11-2012
Hi Pamu, Elixir_sinari,and Alister,
Thanks for your reply. Actually I was trying to set it as array like
Code:
echo "Enter col numbers with space"
        read x
        echo 'You entered this line: ' $x
        set -A rarray $x
        for i in ....

something like this..
But I am stilling struggling to define it such a way. Actually if I want to make it user defined then they should also choose how many col the want to delete. May be sometime 2 or 3 etc. Thus I wanted to read the input as array.

Any suggestion?
Thanks again,
Mitra.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Printing lines with specific strings at specific columns

Hi I have a file which is tab-delimited. Now, I'd like to print the lines which have "chr6" string in both first and second columns. Could anybody help? (3 Replies)
Discussion started by: a_bahreini
3 Replies

2. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

3. Shell Programming and Scripting

Can't figure out how to find specific characters in specific columns

I am trying to find a specific set of characters in a long file. I only want to find the characters in column 265 for 4 bytes. Is there a search for that? I tried cut but couldn't get it to work. Ex. I want to find '9999' in column 265 for 4 bytes. If it is in there, I want it to print... (12 Replies)
Discussion started by: Drenhead
12 Replies

4. Shell Programming and Scripting

How to use awk for deleting columns?

Hi folks, How awk 'll help to do this file contains bunch of insert statement like below remove fourth column from all statement put it in new file INSERT INTO `tbl_medicalquestions` VALUES (1,'Is anyone waiting for an operation, post operative check up, any other hospital treatment or... (12 Replies)
Discussion started by: ashutoshverma
12 Replies

5. Shell Programming and Scripting

deleting columns with NAs

I want to be able to delete columns whose data have more than 10 percent of NAs. x1 x2 x3 x4 1 1 1 1 2 NA 2 2 1 2 1 NA 1 2 1 NA NA 2 1 NA 1 2 1 NA 1 2 1 NA 1 2 1 NA 1 2 1 NA 1 2 1 NA 1 2 1 NA 1 2 1 NA 1 2 1 NA so in this case i will delete x4. lets say there are 100 tables with... (1 Reply)
Discussion started by: johnkim0806
1 Replies

6. UNIX for Advanced & Expert Users

Help in Deleting columns and Renaming Mutliple columns in a .Csv File

Hi All, i have a .Csv file in the below format startTime, endTime, delta, gName, rName, rNumber, m2239max, m2239min, m2239avg, m100016509avg, m100019240max, metric3min, m100019240avg, propValues 11-Mar-2012 00:00:00, 11-Mar-2012 00:05:00, 300.0, vma3550a, a-1_CPU Index<1>, 200237463, 0.0,... (9 Replies)
Discussion started by: mahi_mayu069
9 Replies

7. Shell Programming and Scripting

Deleting columns by list or file

Dear specialists out there, please help a poor awk newbie: I have a very huge file to process consisting of 300000 columns and 1500 rows. About 20000 columns shall be deleted from that file. So it is clear, that I can't do this by writing down all the columns in an awk command like $1, $x etc.... (5 Replies)
Discussion started by: flxms
5 Replies

8. Shell Programming and Scripting

deleting rows that dont have a certain # of columns

Hi, I want to delete rows that dont have a certain # of columns. In my case, rows that are less than 8 should be removed (those greater than 8 are ok). For instance: 1 2 3 4 5 6 7 8 2 3 2 4 3 2 1 5 1 2 3 4 5 6 8 2 2 4 3 1 1 1 1 1 1 1 1 1 after: 1... (8 Replies)
Discussion started by: gisele_l
8 Replies

9. Shell Programming and Scripting

Deleting columns from CSV file

Hi All, Am working on perl script which should delete columns in existing CSV file. If my file is : AA,BB,CC,DD 00,11,22,33 00,55,66,77 00,99,88,21 AA,BB... are all my headers can come in any order (e.g AA,CC,BB...) and rest are values. I want to delete column CC... Can anybody help... (2 Replies)
Discussion started by: darshakraut
2 Replies

10. Shell Programming and Scripting

Deleting specific columns from a file

Hi Friends, I want to delete specific columns from a file. Say my file content is as follows: "1","a","ww1",1234" "2","b","wwr3","2222" "3","c","erre","3333" Now i want to delete the column 2 and 4 from this file. That is I want the file content to be: "1","ww1" "2","wwr3"... (11 Replies)
Discussion started by: premar
11 Replies
Login or Register to Ask a Question