0

I'm not familiar with Windows shell. So, let's say my file is like:

DontAppend this line shouldn't be appended
DontAppend this line shouldn't be either
Some lines
more lines

And I'm appending like this:

type file.txt >> AppendHere.txt

This appends the whole file. How do I make it so it skips lines that begin with "DontAppend"?

Kache
  • 15,647
  • 12
  • 51
  • 79

2 Answers2

3

The command findstr will let you search for lines not containing a string or regular expression so you can use:

findstr /vrc:"^[^A-Za-z0-9]*DontAppend" file.txt >> AppendHere.txt

The /r option says it should use regular expressions and the caret (^) says it should begin with the string.

Edit: added a filter for non alphanumeric chars that may solve the Unicode issues (Unicode files sometimes have a non-printable indicator characters in the beginning).

Kache
  • 15,647
  • 12
  • 51
  • 79
Didi Kohen
  • 560
  • 4
  • 19
  • +1 I have used Windows for years and never used `findstr` before after installing `unixutils` on every box ;o) – didster Oct 11 '12 at 11:59
  • @didster I don't think I ever used it, but I have read about it somewhere in the past so I looked it up. When you're a UNIX guy stuck in a Windows environment and not allowed to use unixutils/cygwin, you learn to find the parallels... – Didi Kohen Oct 11 '12 at 12:07
  • It works for all `DontAppend` lines except for when the file begins with `DontAppend`. Is there a regex for "beginning of file" to `||` them together? – Kache Oct 11 '12 at 12:21
  • I guess it's kind of a bug, that `^` doesn't match "beginning of file" as well. Luckily, `DontAppend` only ever appears at the beginning of a line, so I just removed the caret. – Kache Oct 11 '12 at 12:26
  • To clarify on my previous comment, it may be because my text files are in Unicode. The caret works for ANSI files. – Kache Oct 11 '12 at 12:47
  • ah, haha! That's pretty funny/creative. I tried `"^[^.]*DontAppend"`, and that works too. It's probably even safer that way. – Kache Oct 11 '12 at 17:20
  • You mean safer my way or the one you tried? The `"^[^.]*DontAppend"` **shouldn't** work actually since . is any character and negating it should match nothing, and not match the Unicode control characters. – Didi Kohen Oct 11 '12 at 18:23
  • Hmm, you're right, my mistake. Would you happen to know a way to strip those three Unicode BOM characters before sending it to `>> AppendHere.txt`? – Kache Oct 11 '12 at 22:50
0

Either get grep for windows or you could use Windows' own find command

type so.txt|find /v "DontAppend" >> output.txt

The /v option means output lines that dont match your string.

find works for very simple things like this but any more you will need a real filtering tool like grep

didster
  • 882
  • 5
  • 9