Calling all nerds

Soldato
Joined
8 Oct 2008
Posts
2,688
Location
Hull, East Yorkshire
How do I delete part of a line of texts?

Example

del 2020bbh : 030-c3.c3 (1048576 bytes) - NOT FOUND (2020bb)
del 2020bbh : 030-c4.c4 (1048576 bytes) - NOT FOUND (2020bb)
del 3countb : 043-p1.p1 (1048576 bytes) - NOT FOUND

I need to delete everything on the right after the colon. I have thousands of lines of text that I need to use for a batch script, deleting them manually would take me forever.

I have a big suprise for the first person with the solution!
 
Grab yourself a copy of sublime text 2
, free trial and use the built in search (crtl + f) for ":" then hit find all. It will place a cursor on every line then just highlight everything to the right and hit delete.

Example of how it looks when selecting multiple lines.

sublimetextcurser.gif


Another option which is slightly more complicated but quicker is to search ":.(.+)" ignoring the quotation marks.

This is known as regular expression or regex, it is a way of searching for sequences in code. That expression will search for anything with a ":" and select anything to the right of it. Then hit find all and hit delete. Job Done.
Hit Crtl + F to bring up the search box. Bottom Left hit the button to set it into regular expression search mode.
Screen%20Shot%202013-12-22%20at%2002.23.40.png


In the middle search box type in ":.(.+)" as seen below. It will highlight all the results in a white box. Then hit find all bottom right.
Screen%20Shot%202013-12-22%20at%2002.20.42.png


After hitting find all the search box will disapear but all your results will now be highlighted. Then hit the backspace/del key.
Screen%20Shot%202013-12-22%20at%2002.21.03.png


Your Done! that will have deleted everything after and including the ":" on every line.

Use "[^:]*$" if you wish to keep the ":"
 
Last edited:
How do I delete part of a line of texts?

Example

del 2020bbh : 030-c3.c3 (1048576 bytes) - NOT FOUND (2020bb)
del 2020bbh : 030-c4.c4 (1048576 bytes) - NOT FOUND (2020bb)
del 3countb : 043-p1.p1 (1048576 bytes) - NOT FOUND

I need to delete everything on the right after the colon. I have thousands of lines of text that I need to use for a batch script, deleting them manually would take me forever.

I have a big suprise for the first person with the solution!

del 2020bbh - NOT FOUND (2020bb)
: 030-c4.c4 (1048576 bytes) - NOT FOUND (2020bb)
del 3countb : 043-p1.p1 (1048576 bytes)

Done. Hope that helps
 
^ Hes got thousands of lines to do the same thing to. So although that works for 3 it doesn't fix the other thousand+.
 
lol Derek I'll send you the file and you can do the other hundred thousand ;)

Cypto that worked beautifully! Thanks bro!

Ice cream sandwhich for you!

f3f9.png
 
No worries, glad it worked still getting my head around regex. It's as if you already knew what I wanted for Christmas :) Cheers.

100 thousand... damn I see why you where looking for the easy way to do it.
 
@Echo off
FOR /F "tokens=1* delims=:" %%G IN (a.txt) DO ECHO %%G>>b.txt


input file = a.txt

output to b.txt

del 2220bbh
del 2020bbh
del 3countb
 
Last edited:
For sure regex is one of the most annoying things but it's also one of the most helpful.
Haha that's not chocolate btw.
And that's not ice cream...

Yeah it felt good to see all the first half get wiped out in one smooth move!

lol here come all the other nerds with their own cool methods, you HAVE to show how much you know :p
 
I like list comprehensions. :)

Code:
t = "\n".join([x.split(":")[0]+":" for x in open("in.txt","r").readlines()]);open("out.txt", "w").write(t)
 
What you can also do is create a new line at the top of the data, enter sep=: on the first line.

So it looks like this:
Code:
sep=:
del 2020bbh : 030-c3.c3 (1048576 bytes) - NOT FOUND (2020bb)
del 2020bbh : 030-c4.c4 (1048576 bytes) - NOT FOUND (2020bb)
del 3countb : 043-p1.p1 (1048576 bytes) - NOT FOUND


Then save the text file as .csv, then you can simply open it in excel and everything after the ':' will be in a separate column.

Only works if colons aren't part of the data as well :p
 
Last edited:
Back
Top Bottom