Delete a line that does not contain 1 value
Solved
dpart
-
pitibalrog Posted messages 1 Status Membre -
pitibalrog Posted messages 1 Status Membre -
Hello,
I have a txt file with hundreds of thousands of lines.
I only want to keep the lines that contain the value 42218
Do you have a way?
Knowing that I don't know how to program, of course!
Thank you in advance, your help would be very precious.
I have a txt file with hundreds of thousands of lines.
I only want to keep the lines that contain the value 42218
Do you have a way?
Knowing that I don't know how to program, of course!
Thank you in advance, your help would be very precious.
Configuration: Windows XP Firefox 3.5.3 Word 2002
9 réponses
Hi,
so you’re going to love regular expressions!
Basically, it's a generic way of writing for find and replace.
You need a text editor that supports find and replace with regular expressions, like Notepad++ => https://www.commentcamarche.net/telecharger/developpement/16295-notepad/
Here’s what I think is the easiest process to follow:
1] Mark the lines to keep:
find: ^(.*)(42218)(.*)$
replace: @$1$2$3
2] Delete the lines you don’t want
find: ^[^@]{1}.*$
replace with nothing, we delete:
3] Remove extra line breaks:
find: \r\n\r\n
replace: \r\n
4] Remove the line marking:
find: ^@
replace with nothing, we delete:
There’s a way to do this with fewer expressions, but this is the easiest to write/understand.
Enjoy!
Zep3k!GnO
so you’re going to love regular expressions!
Basically, it's a generic way of writing for find and replace.
You need a text editor that supports find and replace with regular expressions, like Notepad++ => https://www.commentcamarche.net/telecharger/developpement/16295-notepad/
Here’s what I think is the easiest process to follow:
1] Mark the lines to keep:
find: ^(.*)(42218)(.*)$
replace: @$1$2$3
2] Delete the lines you don’t want
find: ^[^@]{1}.*$
replace with nothing, we delete:
3] Remove extra line breaks:
find: \r\n\r\n
replace: \r\n
4] Remove the line marking:
find: ^@
replace with nothing, we delete:
There’s a way to do this with fewer expressions, but this is the easiest to write/understand.
Enjoy!
Zep3k!GnO
We're rangers man, rangers...
If you have Excel, you can do this with
you type Text in the first cell
then you copy your entire file by CTRL+C
you place your cursor in the cell below and paste your entire file by CTRL+V
then select everything and go to Data
Filter..
auto filter, you select contains and next to it you type 42218
and OK, you will only have the rows that interest you
you copy them and paste them wherever you want
you type Text in the first cell
then you copy your entire file by CTRL+C
you place your cursor in the cell below and paste your entire file by CTRL+V
then select everything and go to Data
Filter..
auto filter, you select contains and next to it you type 42218
and OK, you will only have the rows that interest you
you copy them and paste them wherever you want
Yes, thank you, but I have way too many lines for it to work with Excel. Unless I split the txt file into several dozens of others...
That's why I was looking for an automatic way to do it directly in the txt file.
That's why I was looking for an automatic way to do it directly in the txt file.
Hello,
Download sed here https://sourceforge.net/projects/gnuwin32/files/sed/4.2-1/sed-4.2-1-setup.exe/download?use_mirror=iweb
Then install it.
Here is an example of usage http://cjoint.com/data/kttzUEAxlD.htm
It's not complicated ;-)
--
106485010510997108
Download sed here https://sourceforge.net/projects/gnuwin32/files/sed/4.2-1/sed-4.2-1-setup.exe/download?use_mirror=iweb
Then install it.
Here is an example of usage http://cjoint.com/data/kttzUEAxlD.htm
It's not complicated ;-)
--
106485010510997108
What is the purpose?
2nd step: there is nothing happening
Am I doing something wrong?
Step 1: I actually replace the lines with my value by @$1$2$3
What’s the point?
--> 1] We mark the lines to keep:
==> Well, it serves to mark the lines!
Step 2: nothing happens, am I doing something wrong?: Yes, because this line deletes the ones we don't want as I INDICATED in the title of 2]...
overwhelmed...
--
Zep3k!GnO
=> to make it work, you need to check the "regular expression" box at the bottom of the search box
2) Remove all lines that do not start with @
v/42218/d
Which means: delete all lines that do not contain the value "42218"
To keep only the lines containing 42218, you just need to search for the lines that do not contain 42218 and delete them (by replacing them with nothing).
To do this in Notepad++, press ctrl+H on your keyboard to open the find and replace window.
In the "Find what" field, type: ^((?!42218).)*$\n
Leave the "Replace with" field empty
Check "Regular expression" in the search mode
And finally, click on the replace all button. If it doesn't work as you want, press ctrl+z on your keyboard to revert to the previous state.