Deleting specific columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting specific columns
# 1  
Old 09-11-2012
Deleting specific columns

Hi group,
Can you please tell how to delete specific columns from a file.

I know something like
Code:
awk -F, '{ print $1" "$2" "15 }' input.txt > output.txt

will delete all other columns. But this is in a way to copy some particular columns.

But is there any other way to select just some columns to delete?
Thanks
Mitra
# 2  
Old 09-11-2012
Try this..

Code:
awk -F, '{ $1="" }1' input.txt

This User Gave Thanks to pamu For This Post:
# 3  
Old 09-11-2012
Thanks Pamu,
But where should I put the colum number to delete?
# 4  
Old 09-11-2012
Quote:
Originally Posted by smitra
Thanks Pamu,
But where should I put the colum number to delete?
Just give like this.. just change the column name...1 and 2
Code:
awk -F, '{ $1="";$2="" }1' input.txt

# 5  
Old 09-11-2012
It would help you (and others) if you could post a sample input and corresponding sample output so that people know what "delete a column" really means to you. This is to clarify whether you need "those 2 consecutive commas" which might crop up when you set a field to a null value.
# 6  
Old 09-11-2012
Hello,
suppose I have something like:

1 1 5 1 1 5 1 1 5
1 1 1 1 1 1 1 1 1
1 4 1 1 4 1 1 4 1
1 1 3 1 1 3 1 1 3
1 5 1 1 5 1 1 5 1
4 1 1 4 1 1 4 1 1
1 1 4 1 1 4 1 1 4
5 3 5 5 3 5 5 3 5
1 1 1 1 1 1 1 1 1

And now if I want to delete only 3, 5, 7 colums then
Now I know something like
Code:
awk -F, '{ print $1" "$2" "$4" "$6" "$8" "$9"}' input.txt > output.txt

will work

Further I can also use cut as :
Code:
cut -f -2,4, 6, 8- input.txt > output.txt

But these all are indirect option for copying rest of the columns.

So again my question is there any direct option to delete some column.

Actually I want to write e script using user given variable for the columns.

Thanks,
Mitra
# 7  
Old 09-11-2012
These are few....

As per user defined might be lit bit difficult to configure with my scripts..
To remove 3 and 5 column.
Code:
first=3
second=5
awk -v FR="$first" -v SE="$second" '{for(i=1;i<=NF;i++)if(i!=FR&&i!=SE)printf$i OFS;print""}' file

awk '{for(i=1;i<=NF;i++)if(i!=3&&i!=5)printf$i OFS;print""}' file

awk '{$3=$5="";gsub(FS "+",FS)}1' file

This User Gave Thanks to pamu For This Post:
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