142

I have a 900mb log file which I can open in SublimeText 3. This file is bloated with lines similar to the following.

10/08/2014 23:45:31:828,Information,,,,ExportManager: ,No records to send and/or not connected

How can I filter out all the lines which contain No records to send and/or not connected

energ1ser
  • 2,703
  • 4
  • 24
  • 31
  • 1
    While the below solutions work, this type of job should really be done with sed/awk for large files. – Bar May 11 '18 at 12:39

10 Answers10

266

You can do a regular expression search-and-replace:

Click Find > Replace.

Ensure that the Regular Expression button is pressed.

For the Find What field, put:

^.*No records to send and/or not connected.*\n

Leave the Replace With field empty.

Click Replace All

Opal
  • 81,889
  • 28
  • 189
  • 210
Jonathan Aquino
  • 3,780
  • 1
  • 18
  • 19
  • 21
    The full stop/period at the end is important, without it, it won't work. Alternatively, you could use this: `^.*No records to send and/or not connected\S.*$` – Navigatron Mar 09 '15 at 21:37
  • This solution is the only feasible one if you're operating on a large file. My machine got stuck for several minutes when I did `Ctrl+Shift+K` with 200,000 lines selected. – Przemek D Aug 28 '17 at 11:43
  • Imma try this with my 26 000 000 lines file :') – Zoette Mar 22 '18 at 04:43
75

i could not get the regex to work so I used Alt-F3 approach from this answer:

https://superuser.com/questions/452189/how-can-i-filter-a-file-for-lines-containing-a-string-in-sublime-text-2/598999#598999

  1. Select string of interest
  2. Hit Alt+F3 to go into multi-cursor mode on all occurrences (Ctrl+CMD+G on Mac OS X)
  3. Hit Ctrl+L [see comments] (Cmd+L on Mac)
  4. Copy-paste selection to another buffer
  5. Del
Community
  • 1
  • 1
denfromufa
  • 5,610
  • 13
  • 81
  • 138
  • 2
    Two things: (1) You can [link to an individual answer like this](http://superuser.com/questions/452189/how-can-i-filter-a-file-for-lines-containing-a-string-in-sublime-text-2/598999#598999) (2) You should summarize the other answer here (and specialize it to answer this question as appropriate). – Conspicuous Compiler Feb 17 '15 at 20:57
  • 4
    You can replace steps 3 and 4 with `CTRL + L`, which expands selections to whole lines, including line break. – Chnossos Jun 15 '15 at 14:46
  • 2
    For those like me cursed by a MAC key scheme : `Ctrl+Cmd+G` to for multi-cursor on all occurences, `Cmd+L` to expand lines, and the usual copy/paste – Cyril Duchon-Doris Nov 13 '16 at 22:04
73

For people that don't want to write a regex - you can just select the search string, hit ctrl+cmd+g or pick "Quick Find All" from the menu, which will get you selections for each matching string; from there Home will move every selection cursor to the start of the line, shift+End will select every matching line, and del, del will delete all of them.

Multiple cursor editing is fun!

Leonid Shevtsov
  • 14,024
  • 9
  • 51
  • 82
  • 9
    This is a very useful more generalized tip. My objective was to delete all lines containing a string. I ended up selecting one instance of it then used "QuickFind" with the shortcut `Ctrl+Cmd+G` as Leonid suggests for selecting all those strings. After this I could go directly to "Delete Line" `Ctrl-Shift-K` and surgically all the lines with this string were removed in an instant. Sublime Text really is the best text editor I've ever used. – MiB Jun 04 '15 at 21:49
  • 1
    Is ctrl+cmd+g the same as ctrl+alt+g on Windows? I couldn't get this to work – black panda Sep 28 '15 at 10:53
  • 1
    @black panda For windows "Quick Find All" is alt+f3 - Thanks Leonid +1 – 7caifyi Oct 22 '15 at 15:47
  • If you are like me and you do not have a Home button, you can also use CMD + <- (left arrow) – Koray Tugay Jan 27 '16 at 08:04
  • 4
    You can also use cmd + L to expand selection to line. – Koray Tugay Aug 01 '16 at 06:21
36

This is what i found for the windows users:

  1. Select the string (every line containing this string is to be removed).
  2. Press ALT+F3 .
  3. Press Ctrl+L .
  4. Press Delete .
Prashant Goel
  • 471
  • 4
  • 7
26

Neither of the regex code suggested above worked in my case, but this did work:

.*(text in question).*
Magnus
  • 261
  • 3
  • 3
  • 1
    It will let empty lines on your code, is that you want to? If not, add `\n` or `$` at the end: `.*(text in question).*\n` – Evandro Coan Dec 04 '16 at 00:31
  • If lines are having "abc/xyz/something" like structure, to use regex add ' \ '. For example if we have to search anything in between abc/ and /something then the expression will be : abc/\.*.*\/something – Avik Nov 14 '17 at 10:54
  • 1
    Works for me, If you need multiple matches like I did, use `|` like so: `.*(a).*|.*(b).*|.*(c).*` – bryc Jan 02 '18 at 13:23
8

A simple way of doing it is:

  • 1 Open Sublime Text
  • 2 Find => Replace (Ctrl + H)
  • 3 in Find write the desired text
  • 4 click Find All
  • 5 press ctrl + shift + K to remove all the lines where this search is present

This is a quick solution to remove some lines that contains some text

Shuher
  • 108
  • 1
  • 6
1

Above answers are the correct ways, but if you want to get rid of the rows with even a single string then do, Find -> Replace -> put ^.*[a-zA-Z]+.*\n In the find section and keep replace with blank. Hit the replace all button this will delete all the rows with even a single string in it.

shubham mishra
  • 1,183
  • 14
  • 21
1

I like the manual edition solution, very good.

But.. have you tried to use cat and grep -v to filter out the lines and redirect to another file? Maybe better than learning regex.. (personally I always start with regex and end with editing the files myself).

In Windows you use findstr /v.

So you would do:

# in bash
cat my.log | grep -v "No records to send and/or not connected" > new.log

or

# in cmd
cat my.log | findstr /v "No records to send and/or not connected" > new.log
WesternGun
  • 11,303
  • 6
  • 88
  • 157
0

I ran into a similar problem editing a sitemap

This worked for me:

  1. Copy the last word in the lines that you want to delete
  2. Find all
  3. Press delete to delete the entire line
Pete Varley
  • 412
  • 5
  • 11
-1

Find -> Find all (this will mark the lines having the keyword) Then go to Edit->Line->Delete line

Pratyush
  • 19
  • 1
  • 2