Parsing a CSV file and deleting all rows on condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing a CSV file and deleting all rows on condition
# 1  
Old 06-07-2011
Java Parsing a CSV file and deleting all rows on condition

Hello list,

I am working on a csv file which contains two fields per record which contain IP addresses. What I am trying to do is find records which have identical fields(IP addresses) which occur 4(four) times, and if they do, delete all records with that specific identical field(ip address).

for example, the csv looks like this:
Code:
field 1,192.168.1.1,field 3, 192.168.1.128
field 1,192.168.1.1,field 3, 192.168.1.128
field 1,192.168.1.1,field 3, 192.168.1.128
field 1,192.168.1.1,field 3, 192.168.1.128
field 1,10.10.10.1,field 3, 192.168.1.128
field 1,172.16.10.1,field 3, 192.168.1.128
field 1,10.10.10.3,field 3, 192.168.1.128

so I want to parse the csv and delete the following rows where the ip address in field 2 is identical for 4 occurrences:
Code:
field 1,192.168.1.1,field 3, 192.168.1.128
field 1,192.168.1.1,field 3, 192.168.1.128
field 1,192.168.1.1,field 3, 192.168.1.128
field 1,192.168.1.1,field 3, 192.168.1.128

any ideas?

thanks.

Last edited by Franklin52; 06-08-2011 at 04:01 AM.. Reason: Please use code tags
# 2  
Old 06-07-2011
Code:
awk -F , 'NR==FNR{a[$0]++;next} a[$0]<4' infile infile

field 1,10.10.10.1,field 3, 192.168.1.128
field 1,172.16.10.1,field 3, 192.168.1.128
field 1,10.10.10.3,field 3, 192.168.1.128

This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 06-08-2011
hi rdcwayx,

I tried this but can't seem to get it to work. Also I've only got one infile, so I'm not comparing two different files, but rather comparing records, and if there are 4 records with the exact same field value for one of the specific fields, then deleting all four records. Something like an < infile > outfile function.
# 4  
Old 06-08-2011
Try this, Pass input file twice
Code:
awk -F"," 'NR==FNR{a[$2]=a[$2]+1;next}a[$2]<4' infile infile >outfile

This User Gave Thanks to pravin27 For This Post:
# 5  
Old 06-08-2011
it works!
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

Read column from file and delete rows with some condition..

Hi.... I have a need of script to do delete row whenever condition is true.... 2.16 (3) 1 3 9999 0 (1) (0) 34.42 (4) 1 3 9999 37 (2) (3) 34.38 (4) 1 3 9999 64 (2) (3) 34.4 (4) 1 3 1 ... (13 Replies)
Discussion started by: nex_asp
13 Replies

3. Shell Programming and Scripting

deleting rows under a certain condition

there are 20 variables and I would like to delete the rows if 13th-20th columns are all NA. Thank you! FID IID aspirpre statihos fibrahos ocholhos arbhos betabhos alphbhos cacbhos diurehos numbcig.x toast1 toast2 toast3 toast4 ischoth1 ischoth2 ischoth3 ischoth4 101 101 1 1 1 1 1 2 1 2... (2 Replies)
Discussion started by: johnkim0806
2 Replies

4. UNIX for Dummies Questions & Answers

Deleting specific rows from a text file

How do I go about deleting specific rows from a text file (given row number)? (5 Replies)
Discussion started by: evelibertine
5 Replies

5. UNIX for Dummies Questions & Answers

Help with deleting specific rows from a text file

I know this is a complicated question but I will try to illustrate it with some data. I have a data file that looks like the following: 1341 NA06985 0 0 2 46.6432798439 1341 NA06991 NA06993 NA06985 2 48.8478948517 1341 NA06993 0 0 1 45.8022601455 1340 NA06994 0 0 1 48.780669145 1340... (1 Reply)
Discussion started by: evelibertine
1 Replies

6. Shell Programming and Scripting

2 problems: Mailing CSV file / parsing CSV for display

I have been trying to find a good solution for this seemingly simple task for 2 days, and I'm giving up and posting a thread. I hope someone can help me out! I'm on HPUX, using sqlplus, mailx, awk, have some other tools available, but can't install stuff that isn't already in place (without a... (6 Replies)
Discussion started by: soldstatic
6 Replies

7. Shell Programming and Scripting

Deleting specific rows in large files having rows greater than 100000

Hi Guys, I need help in modifying a large text file containing more than 1-2 lakh rows of data using unix commands. I am quite new to the unix language the text file contains data in a pipe delimited format sdfsdfs sdfsdfsd START_ROW sdfsd|sdfsdfsd|sdfsdfasdf|sdfsadf|sdfasdf... (9 Replies)
Discussion started by: manish2009
9 Replies

8. Shell Programming and Scripting

Deleting rows from csv file

Hello, I am supposed to process about 100 csv files. But these files have some extra lines at the bottom of the file. these extra lines start with a header for each column and then some values below. These lines are actually a summary of the actual data and not supposed to be processed. These... (8 Replies)
Discussion started by: cobroraj
8 Replies

9. Shell Programming and Scripting

deleting rows & columns form a csv file

Hi , I want to delete some rows & columns from file. can someone please help me on this? Regards. (2 Replies)
Discussion started by: code19
2 Replies

10. Shell Programming and Scripting

Deleting the emty rows in a file

I am getting some spaces between the two lines(rows) in file.i want delete that empty rows in the file example 1 abc xyz 2 def jkl like i am having lots of rows in a file i want to delete the spce between the two rows give any... (7 Replies)
Discussion started by: srivsn
7 Replies
Login or Register to Ask a Question