How to use awk for deleting columns?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use awk for deleting columns?
# 1  
Old 07-10-2012
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

Code:
INSERT INTO `tbl_medicalquestions` VALUES (1,'Is anyone waiting for an operation, post operative check up, any other hospital treatment or any medical investigations, tests or test results (for anything other than pregnancy)?',0,0,'2010-10-19 11:44:44');


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by Scrutinizer; 07-11-2012 at 03:20 AM.. Reason: code tags, see PM
# 2  
Old 07-10-2012
What is the column that you are talking about? Smilie
# 3  
Old 07-11-2012
if you check the insert statement ----)?',0,0 second zero stand for the fourth column like this I have to delete it

---------- Post updated 07-11-12 at 12:23 AM ---------- Previous update was 07-10-12 at 09:25 PM ----------

Problem of deleting columns from insert statements...can you help me out
# 4  
Old 07-11-2012
Code:
 
awk -F"\'," -v OFS="\'," '{sub(/,.*,/,",",$2)}1' input.txt

This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 07-11-2012
Try:
Code:
awk '{$(NF-1)=$NF; NF--}1' FS=, OFS=, infile

--
or
Code:
awk '$NF=$(NF--)' FS=, OFS=, infile

Smilie

Last edited by Scrutinizer; 07-11-2012 at 03:37 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 07-11-2012
sorry back again I ran this command I only want to delete fourth column from this insert statement (INSERT INTO `tbl_medicalquestions` VALUES ) there are other insert statements too in the file which I don't want to delete
grep '`tbl_medicalquestions`' insert_script20120711.txt | awk -F"\'," -v OFS="\'," '{sub(/,.*,/,",",$2)}1' insert_script20120711.txt > insert_scriptold.txt

---------- Post updated at 06:48 AM ---------- Previous update was at 06:22 AM ----------

Any one can help me out please....
# 7  
Old 07-11-2012
How about this, then?
Code:
 
awk -F"\'," -v OFS="\'," '/tbl_medicalquestions/ {sub(/,.*,/,",",$2)}1' input.txt

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Deleting specific columns

Hi group, Can you please tell how to delete specific columns from a file. I know something like 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... (11 Replies)
Discussion started by: smitra
11 Replies

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

4. Shell Programming and Scripting

Deleting columns passed as arguments to the script

Hi all, I am trying to delete columns in a file using a script. The columns that need to be deleted are passed as arguments to the script. The script should look like this > delete_columns.sh <file_name.txt> <column_numbers_to_be_deleted> The contents of the file_name.txt will be like ... (5 Replies)
Discussion started by: VNR
5 Replies

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

6. UNIX for Dummies Questions & Answers

Deleting all rows with empty columns

I have a text file that looks like this: 1 rs523634 8.22486 1 1 rs585160 8.22488 1 rs497228 8.2249 1 1 rs600933 8.225 1 rs480106 8.22531 1 rs600199 8.22533 1 rs529015 8.22534 1 rs598894 8.22534 I want to delete the rows with empty... (2 Replies)
Discussion started by: evelibertine
2 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