DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: delete all the lines that do NOT start with a specific string in multiple files  (Read 13,281 times)

kalos

  • Member
  • Joined in 2006
  • **
  • Posts: 1,824
hello

I have an amount of text files and I want to delete all the lines that do NOT start with a specific string

how can I do this?

thanks

EDIT: the program must support unicode characters

housetier

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 1,321
    • Gathering Badge
on linux a combination of grep and the shell's for loop could accomplish this:

Code
export BAN="whatever should not be kept"
mkdir after
for textfile in *.txt; do grep $textfile -v ^$BAN > after/$textfile; done

This should work if the files all have a .txt extension.

"grep -v" prints lines that do NOT match; "^" means beginning-of-line; "$BAN" will be replaced by whatever you put between the quotation marks in the export statement above. The output from grep is then put into a new file with the same filename, but in a different directory.

If you have cygwin, you can do this under windows as well.

bgd77

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 203
kalos, could you specify if this is for Windows or Linux?

TucknDar

  • Advanced coder of Nowt
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 1,133
edit:
completely misunderstood... sorry

kalos

  • Member
  • Joined in 2006
  • **
  • Posts: 1,824
it's for winxp
thanks

Ehtyar

  • That News Guy
  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 1,237
    • Plugin Coding Medal
    • Gathering Badge
Well, I now despise Windows for having such a totally sucky console, but I managed to get this working.

As an exercise for myself I compiled GNU grep myself. It required a couple of patched but all-in-all relatively simple. If you're not capable of this yourself, you can get an only slightly outdated binary from UnxUtils (I've included my build in the attached zip). Converting the shell script to batch was a huuuge pain in the ass because (again) the Windows console sucks, and doesn't honor piping symbols in a for loop in the same way it would as a standalone command (you have to enclose your command in parens prior to the >, < or | symbol). God help you trying to find that in Google (I ended up randomly guessing it). Finally, grep on Windows converts CRLF line endings to plain LF (for internal operational reasons), so we need the todos utility to convert them back again.

So here you are, give it a try and let me know how it goes.

Ehtyar.

[edit]
Er...should this not be moved to the coding snack new request forum?
[/edit]

[edit2]
If you have cygwin, you can do this under windows as well.
MSYS will manage it also, minus the enormity of Cygwin.
[/edit2]
« Last Edit: March 14, 2009, 10:49 PM by Ehtyar »

bgd77

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 203
I think that the only way to do it is how Ehtyar suggested. i'm no expert in Windows Command Prompt, but I do not think that it is powerful enough to make changes in files. If someone knows better, please correct me!

One alternative would be to install ActivePerl or Python and to write a small program to do this, if you think that all the trouble is worthwhile.

Ehtyar

  • That News Guy
  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 1,237
    • Plugin Coding Medal
    • Gathering Badge
*sigh*, life would indeed be easier if Windows came with a posix-compatible shell or Strawberry Perl, but alas, here I am passing around utilities that others would've had on almost any other operating system. I think, however, that this is a debate for another day.

kalos, would you let us know how it goes please?

Ehtyar.

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 3,217
Converting the shell script to batch was a huuuge pain in the ass because (again) the Windows console sucks, and doesn't honor piping symbols in a for loop in the same way it would as a standalone command

Ehtyar,

Take a look at Horst Schaeffer's List MODifier LMOD, which is a great workaround for making batch files to get round these kind of problems.

Ehtyar

  • That News Guy
  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 1,237
    • Plugin Coding Medal
    • Gathering Badge
I do like the sound of that utility rjbull, thanks for recommending it. However, the problem is no longer a problem, as I found the solution. I was complaining about the lack of documentation regarding the Windows console. Perhaps my searching skills failed me, but if you have to resort to guessing the syntax of any language, the documentation is in pretty poor shape.

Ehtyar.

David1904

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 43
TextHarvest is useful for keeping or deleting lines from text files. It is very simple to use.
You can find it on Tucows or other download sites.

TheQwerty

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 84
I believe since Windows NT there's been a command, findstr, which acts similar to grep, but I'm not sure about the support for Unicode.

You should be able to do something like this:
Code
findstr /V "^needle" input.txt>output.txt
The /V tells it to print the lines that do not match the regular expression "^needle" (any line beginning with "needle").

bgd77

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 203
I've tried it and searched some forums. findstr does not support unicode.  :down:

So, find must be used, and it seems to be really complicated to use it for this purpose.

bgd77

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 203
I think that this can be easily done with Windows Power Shell. Does anyone know more things about it?

app103

  • That scary taskbar girl
  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 5,888
    • Participated in NANY Challenge Six Times
    • Coding Snacks
    • Gathering Badge
    • Cody Award
    • Volunteers/Staff
Could probably do it with WSH and javascript too.

Krishean

  • I like pie
  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 75
    • Participated in NANY Challenge Three Times
i could do this in javascript/wsh, i just need to know:

1. how large will the files be
2. how much memory are you willing to let the script use (more memory = faster, less = slower)

Any sufficiently advanced technology is indistinguishable from magic.

- Arthur C. Clarke

Original address: https://www.donationcoder.com/forum/index.php?topic=17449.0