Monday 15 August 2011

c# - Deleting the rest of the lines in a text file after a line contains a specific string -



c# - Deleting the rest of the lines in a text file after a line contains a specific string -

i have text file (incoming_orders.log) beingness written api , need maintain orders come in before time while incoming_orders.log still beingness written too.

example of incomimg_orders.log:

orderid=1,time=13:02:04,quantity=7,part=8

orderid=2,time=13:04:47,quantity=16,part=1

orderid=3,time=13:05:00,quantity=5,part=2

orderid=4,time=13:05:01,quantity=86,part=11

orderid=5,time=14:58:04,quantity=21,part=19

orderid=6,time=14:22:40,quantity=7,part=32

orderid=7,time=16:15:17,quantity=7,part=32

orderid=8,time=16:21:23,quantity=7,part=32

orderid=9,time=16:35:44,quantity=7,part=32 ...

i need orders @ or before 13:05:00 , write them new file;

i have tried following, gets rid of orderid 4 , keeps rest

string newfile = @"path.log"; string incoming_orders = @"incoming_orders.log" var oldlines = system.io.file.readalllines(incoming_orders); var newlines = oldlines.where(line => !line.contains("time=13:05:01")); system.io.file.writealllines(newfile, newlines);

please allow me know else can add together clarification.

thanks

change var newlines... line this:

var newlines = oldlines.where(line => line.split(',')[1].compareto("time=13:05:01") < 0);

given sample lines above, resulted in next new file content:

orderid=1,time=13:02:04,quantity=7,part=8 orderid=2,time=13:04:47,quantity=16,part=1 orderid=3,time=13:05:00,quantity=5,part=2

c# visual-studio-2010 visual-studio text-files

No comments:

Post a Comment