Delete a line that does not contain 1 value
Solved
dpart
-
pitibalrog Posted messages 1 Status Member -
pitibalrog Posted messages 1 Status Member -
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 answers
-
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!GnOWe're rangers man, rangers...
-
1st step: I actually replace the lines with my value by @$1$2$3
What is the purpose?
2nd step: there is nothing happening
Am I doing something wrong?- I don't know, I must be writing wrong or expressing myself poorly because it's the simplest it gets...
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!GnOWe are rangers man, rangers...
-
-
-
The topic is a bit old but when I see that people are talking about Notepad++ with regex, I find it amazing. However, when it is suggested to do this in 4 steps instead of one, I understand why it might be perplexing.
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.
-
-
hello
I only want to keep the first line, which contains "42218"
in DOS
findstr "42218" file.txt > file2.txt -
Hello,
With sed (the original file will be saved with the name fichier.txt.txt)
sed -i.txt '/42218/!d' fichier.txt
--
106485010510997108 -
-
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 -
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. -
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 -
Wow, nice! I didn't know that, it's awesome! :)
--
Zep3k!GnOWe're rangers, man, rangers...
-