Die einfachste Möglichkeit, unerwünschte Zeilen aus einer großen Textdatei zu entfernen

3015
Shawn

Ich habe eine große Textdatei mit einer Größe von mehr als 30 Megabyte. Ich möchte alle Zeilen entfernen, die bestimmten Kriterien nicht entsprechen, z. B. Zeilen, die nicht die Zeichenfolge "START" haben.

Was ist der einfachste Weg, dies zu tun?

2
Welches Betriebssystem verwenden Sie? Unfundednut vor 13 Jahren 0
@ MrStatic: MacOS Shawn vor 13 Jahren 0

5 Antworten auf die Frage

3
Doug Harris

If the pattern is really that simple, grep -v will work:

grep -v START bigfile.txt > newfile.txt 

newfile.txt will have everything from bigfile.txt except lines with "START".

(In case it isn't obvious, this is something you'll do in Terminal or other command line tool)

2
whatever

The original question asked how to remove the lines that didn't match a pattern. In other words, how to keep the lines that do match the pattern. Thus, no need for -v.

grep START infile.txt > outfile.txt 

Note that grep can use regular expressions to do much more powerful pattern matching. The syntax is a bit obtuse though.

1
Ignacio Vazquez-Abrams

Use GNU sed with the -i argument.

um die Antwort etwas mehr zu machen .. verbose: "sed -n -e '/ START / p' inputfile". und vielleicht ist es eine gute Idee, -i nicht zu verwenden, während Sie mit dateibasierenden Befehlen herumspielen, nur für den Fall. akira vor 13 Jahren 2
1
sleske
grep -v START inputfile 

should work. grep is standard on both MacOS and Linux/Unix, can be installed on MS Windows.

Option -v is for inverting the match - only output lines that do not contain the pattern (the inverse of the usual grep behaviour).

1
Mike Fitzpatrick

For Windows Command Prompt (help find for options):

find /v "START" original_file.txt > new_file.txt 

For Linux, OS X, etc. (man grep for options):

grep -v "START" original_file.txt > new_file.txt 

For more complicated text matching grep offers a lot more functionality than find. If you are on Windows you can easily find a port of grep or you can use Windows' findstr instead of find.