How can I delete everything after the first column in Notepad++?

40

16

I'm trying to get rid of everything after a column in Notepad++. Column mode is not an option. Is it possible?

What I have:

70.97.110.40    159 ms          [n/a]                   21              
70.97.117.177   134 ms          [n/a]                   21              
70.97.120.10    75 ms           [n/a]                   21              
70.97.122.105   87 ms           www.portless.net        21              
70.97.122.106   89 ms           www.popovetsky.org      21              
70.97.122.107   95 ms           www.psmythe.net         21              
70.97.122.104   98 ms           wasabi.prostructure.com 21              
70.97.122.108   89 ms           crm.prostructure.com    21              
70.97.122.109   87 ms           internal.prostructure.com21 

What I want:

70.97.110.40
70.97.117.177
70.97.120.10
70.97.122.105
70.97.122.106
70.97.122.107
70.97.122.104
70.97.122.108
70.97.122.109

Bob J

Posted 2014-08-19T18:06:59.457

Reputation: 511

2Are the columns tab-separated? – Excellll – 2014-08-19T18:15:49.620

no theyre white spaces – Bob J – 2014-08-19T18:23:12.580

3One more question: does the data you want to keep ever include a space? – Excellll – 2014-08-19T18:26:24.060

1nope i only need the ips – Bob J – 2014-08-19T18:31:20.173

20Why you want to do that in notepad++? Why column mode is not an option? – Kamil – 2014-08-19T18:36:34.723

1You could use column mode to delete the second column to the EOL, and then use the Trim Trailing and save to get rid of the whitespace. – Casey Kuball – 2014-08-20T21:58:19.543

Answers

60

If the data in the first column never contains a space, you can use a regular expression find and replace to get what you want.

In regular expression mode, search for:

^([^ ]*).*

And replace with

\1

What this does:

^ indicates that any match should start at the beginning of a line.
([^ ]*) is any expression that does not contain a space. The match is greedy, so this will match everything up to the first space (or the end of the line, whichever comes first).
.* is everything else on the line.

\1 refers to the part of the match inside the parentheses. That is, the entire line is replaced by just the bit from the first column.

Excellll

Posted 2014-08-19T18:06:59.457

Reputation: 11 857

5Nice to know that notepad++ supports regular expressions in search-replace! – Kamil – 2014-08-19T18:37:19.400

1Regex will do the job for you so long as the data never contains a space. Simpler would be the UNIX 'cut' command but doing UNIX commands under windows is whole 'nother problem. cut is made for this and processes large files quickly, with defined column delimiters or by character-counts. – Karl – 2014-08-19T18:39:20.680

31This can be done far more simply: replace " .*" (note the leading space) with "" (i.e. nothing) to delete everything after and including the first space. – Fraxtil – 2014-08-19T20:52:15.933

2@Fraxtil Thanks. You should post that as an answer. You'll get an upvote from me. – Excellll – 2014-08-19T21:11:56.173

@Excellll alright, done. – Fraxtil – 2014-08-19T21:19:23.050

99

Here's a simpler regular expression you can use to achieve the same effect. Replace

 .*

(note the leading space)

with nothing. This will delete everything after and including the first space. This will work as long as your IP addresses are never prefixed with any whitespace (as is the case in your example).

Fraxtil

Posted 2014-08-19T18:06:59.457

Reputation: 1 013

8Man, regex is pure magic. I don't think I'll ever grok it :) – Torben Gundtofte-Bruun – 2014-08-20T07:35:35.143

14Make sure that the flag to have . match newlines is off. – Taemyr – 2014-08-20T09:00:56.163

@TorbenGundtofte-Bruun http://regex101.com/ makes everything 100X easier, http://regex101.com/r/xF2pM4/1 example using that website using the above example

– Halfwarr – 2014-08-20T17:38:45.180

3In case it's potentially a tab instead of a space, you can use [ \t].* – Cornstalks – 2014-08-20T18:13:30.783

4

@Cornstalks \s.* is shorter. Maybe even use \s+.*, to match an arbitrary amount of whitespace, now that we're coding defensively.

– Anko – 2014-08-22T13:32:57.800

@Anko: \s can match newlines. – Cornstalks – 2014-08-22T14:08:25.663

@TorbenGundtofte-Bruun No, regex is math. It's all based on formal language theory. Try taking a basic course on writing a compiler. Things will start falling into place. – jpmc26 – 2014-08-22T18:00:56.277

1@jpmc26 as long as you don't say "regex" and "simple" in one sentence :) – Torben Gundtofte-Bruun – 2014-08-22T18:26:36.850

39

If you are using Windows and if you don't mind trailing spaces (you can find/replace them afterwards), use the Block Select feature:

  1. Press and Hold the Alt key
  2. Using the mouse, select the portion to delete (the entire block of text)
  3. Release the Alt key
  4. Delete that block of text
  5. Repeat as necessary

Reed Shilts

Posted 2014-08-19T18:06:59.457

Reputation: 491

Is that supposed to be the minus "-" key? – Dracs – 2014-08-19T23:11:16.683

Sorry - I used LT and GT brackets around 'alt' key. Fixed now. – Reed Shilts – 2014-08-19T23:26:07.683

10You can change it to <kdb>Alt</kbd> and it will format it as a key. – Dracs – 2014-08-19T23:27:33.847

I've been using Notepad forever....and never knew this feature existed! 1 Bazillion +1!!!!! – None – 2014-08-22T22:00:09.533

1The OP said: "Column mode is not an option" – Peter Mortensen – 2014-08-23T18:52:50.047

You can also remove trailing spaces in Notepad++ using Edit > Blank Operations > Trim Trailing Space. – KOVIKO – 2014-10-26T15:35:30.040

11

A regular expression is quicker, but you can do some really tricky things with macros if it is a more complicated task.

You could record a macro:

  1. Cursor on line 1
  2. Hit Start recording
  3. Press key home
  4. Hold Ctrl while you press key Right arrow (seven times)
  5. Hold Shift while you press key End
  6. Press key Delete
  7. Press key Down arrow
  8. Hit Stop recording

Then play it back:

  1. Hit "Run a macro multiple times"
  2. Type in the lines of the document minus 1, since the first line is complete.

pyker

Posted 2014-08-19T18:06:59.457

Reputation: 174

9

Place cursor after 70.97.110.40.

Place cursor

Press Alt, and drag the cursor to right and down to select the unwanted part.

Select

Press Backspace or Delete.

Delete

Keshava GN

Posted 2014-08-19T18:06:59.457

Reputation: 345

1All this time using Notepad++ and I never knew it had a "Block Select Mode" like TextPad. – Kev – 2014-08-23T02:25:01.010

@Kev: it works the same way in Visual Studio. – Peter Mortensen – 2014-08-23T18:51:55.167

1The OP said: "Column mode is not an option" – Peter Mortensen – 2014-08-23T18:54:11.610

@PeterMortensen - re:VS - hell, been using that since 2002 and didn't even know! – Kev – 2014-08-23T22:08:13.347

8

Here are a couple of alternative methods to the given answer.

If you data is fixed width, you can also use the following regular expression in the search:

Find what: ^(.{16}).*$

Replace with: \1

It is great if the data you want to keep does contain a space, as someone mentioned. Additionally, again for fixed width data, you can use Column Mode Editing. It's a neat little feature that allows you to edit multiple lines of data in the same column location. See Column Mode Editing.

It makes mention of arrow keys, but you can also use Page Up/Down for a faster method, as well as End and Home keys. The mouse also works. But you could use this for an ad hoc method of deleting data after a certain point, especially if it is only a few lines. It is also good for copying data, but the way it pastes is a bit odd, so you'd want to get used to that first.

Phillip

Posted 2014-08-19T18:06:59.457

Reputation: 259

1Better than selecting it and replacing it with itself would be to use a lookbehind like: (?<=^.{13}).* and replace it with nothing. This will select anything after the first 13 characters. – Chris Murray – 2014-08-21T15:37:36.443

4

  1. Press ALTwhile using the mouse as a lasso.
  2. Select all you want to remove
  3. Press DELETE

actXc

Posted 2014-08-19T18:06:59.457

Reputation: 41

Assuming the first column lines up nicely, like IP addresses, then this is the quickest way. – bgStack15 – 2014-08-20T14:46:38.737

The OP said: "Column mode is not an option" – Peter Mortensen – 2014-08-23T18:53:23.850

4

Similar to enthdegree:

  1. Place cursor at column 16
  2. Shift + Alt + Page Down
  3. Shift + Alt + End
  4. Delete

If there is a blank line at the end of the file, simply press Up Arrow prior to #3.

It's not perfect, but it's handy.

The Cog

Posted 2014-08-19T18:06:59.457

Reputation: 141

3

Here is a visual way:

  • Put cursor at end of an IP address.
  • Alt+Shift+Pg Down
  • Alt+Shift+Right
  • Delete

This is not 'column based.' It only works because your IP addresses all line up.

enthdegree

Posted 2014-08-19T18:06:59.457

Reputation: 1 280

1What do you mean by "This is not 'column based.'"? Alt+Shift+Arrow switches to "column mode". – MrWhite – 2014-08-20T15:27:30.893

It doesn't respect tab-delineation as a column. It breaks them up into spaces so that each line in the selection has the same # of chars beyond the edge of the selections starting point. – enthdegree – 2014-08-20T15:31:25.073

2

In regular expression mode, search for:

\t.*

And replace with

Nothing

Karthik Vuppala

Posted 2014-08-19T18:06:59.457

Reputation: 21

Can you add a sentence or two to explain what this does? Thanks. – fixer1234 – 2014-12-30T21:55:41.103

1Stack Exchange converts tabs to spaces while displaying, but even in edit mode, the example does not show any tabs. – Arjan – 2014-12-30T22:22:08.507

Thanks this worked excellent in my case. Therefor upping! – Joanne – 2019-12-12T10:31:52.457

0

Delete everything after first column in Notepad++:

  1. Selcect the 2nd column : Alt + Drag
  2. For selecting rest columns: simply press Right Arrow

Ipsita

Posted 2014-08-19T18:06:59.457

Reputation: 1