Delete a line that does not contain 1 value

Solved
dpart -  
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.
Configuration: Windows XP Firefox 3.5.3 Word 2002

9 réponses

Zep3k!GnO Posted messages 2049 Status Membre 200
 
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
We're rangers man, rangers...
11
dpart
 
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?
0
Zep3k!GnO Posted messages 2049 Status Membre 200 > dpart
 
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!GnO
We are rangers man, rangers...
0
pls
 
1) Add an @ at the beginning of each line to keep
=> 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 @
0
Archimed
 
There is a much simpler way with regular expressions (ExpReg):

v/42218/d

Which means: delete all lines that do not contain the value "42218"
0
pitibalrog Posted messages 1 Status Membre
 
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.
0
dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 656
 
hello
I only want to keep the first line, which contains "42218"
in DOS
findstr "42218" file.txt > file2.txt
8
lami20j Posted messages 21506 Registration date   Status Modérateur, Contributeur sécurité Last intervention   3 570
 
Hi,

Thank you ;-)
--
106485010510997108
0
lami20j Posted messages 21506 Registration date   Status Modérateur, Contributeur sécurité Last intervention   3 570
 
Hello,
With sed (the original file will be saved with the name fichier.txt.txt)

sed -i.txt '/42218/!d' fichier.txt

--
106485010510997108
1
dpart
 
Well, thanks, but for me, "sed" is Chinese...
0
dpart
 

01004;Ambérieu-en-Bugey;42218;Saint-Étienne;14,966229
0
Alain_42 Posted messages 5413 Status Membre 904
 
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
0
dpart
 
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.
0
lami20j Posted messages 21506 Registration date   Status Modérateur, Contributeur sécurité Last intervention   3 570
 
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
0
dpart
 
Great, it's perfect, thank you!
0
Zep3k!GnO Posted messages 2049 Status Membre 200
 
Wow, nice! I didn't know that, it's awesome! :)
--

Zep3k!GnO
We're rangers, man, rangers...
0
dpart
 
Thank you all!
0