Using findstr /v on a string enclosed by quotation marks

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
gunitinug
Posts: 9
Joined: 09 Nov 2017 20:04

Using findstr /v on a string enclosed by quotation marks

#1 Post by gunitinug » 18 Nov 2017 03:39

I echo hi logan^^ to terminal. I want to remove ^^ at the end
The following works as intended...

Code: Select all

C:\Users\Logan>echo hi logan^^ | findstr /v "^^^^$"
hi logan
But if I echo "hi logan^^" it doesnt work as I intended...

Code: Select all

C:\Users\Logan>echo "hi logan^^" | findstr /v "^^^^""$"
"hi logan^^"
I want it to return

Code: Select all

"hi logan

THX

penpen
Expert
Posts: 1996
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Using findstr /v on a string enclosed by quotation marks

#2 Post by penpen » 18 Nov 2017 08:28

You cannot use findstr to eliminate characters within a single line.
The reason why the CIRCUMFLEX ACCENT ('^') is disappearing is because it is an escape character, that disappears when a command is interpreted (escaping the next character consuming the escape character):
First the two accents ("^^") are reduced to one ("^") when interpreting the complete line creating the pipe and then executing the single commands (on the left and right), and
then the single one disappears when interpreting the first command.
Because both accent characters were consumed when escaping characters, no circumflex accent passed through the pipe.

You could test it by creating a file "test.txt":

Code: Select all

hi logan^
Then just use this command:

Code: Select all

type test.txt | findstr /v "^^^^$"
The accents seem to appear when using doublequotes, because doublequotes escape all characters between a matching pair, so the accents are not treated as escape characters.

Beside this the circumflex accent also is a special character in findstr, which simply means "start of line".
Actually findstr doesn't display lines with four "starts of line" and one "end of line" - which matches any line because no line could have two successive line starts without a line end.


penpen

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Using findstr /v on a string enclosed by quotation marks

#3 Post by Squashman » 18 Nov 2017 20:43

If you need inline stream editing then look at using JREPL.

You could also download a version of SED that works for Windows as well.

Another popular find and replace utility is FART. But JREPL is much more powerful.

gunitinug
Posts: 9
Joined: 09 Nov 2017 20:04

Re: Using findstr /v on a string enclosed by quotation marks

#4 Post by gunitinug » 19 Nov 2017 16:41

I understand... I got confused... Now I understand findstr works on lines.

But I have another question about findstr /v.

I have persons.txt

Code: Select all

person1^
person1
person2^
person2
Now I try

Code: Select all

C:\Users\CMY\Desktop\test batch scripts\findstr\v switch>type persons.txt | findstr /v "^^$"
person1^
person1
person2^
person2
I was expecting

Code: Select all

person1
person2
These work as expected

Code: Select all

C:\Users\CMY\Desktop\test batch scripts\findstr\v switch>type persons.txt | findstr /r "[1-9]$"
person1
person2

C:\Users\CMY\Desktop\test batch scripts\findstr\v switch>type persons.txt | findstr /v "[1-9]$"
person1^
person2^
THX

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Using findstr /v on a string enclosed by quotation marks

#5 Post by Squashman » 19 Nov 2017 16:48

Read the help file. It should be evident what the /V switch does

gunitinug
Posts: 9
Joined: 09 Nov 2017 20:04

Re: Using findstr /v on a string enclosed by quotation marks

#6 Post by gunitinug » 19 Nov 2017 16:49

Squashman wrote:Read the help file. It should be evident what the /V switch does
/V Print only lines that do NOT contain a match.

I already read it.

I also looked at http://www.robvanderwoude.com/escapechars.php
for escaping ^ character.

gunitinug
Posts: 9
Joined: 09 Nov 2017 20:04

Re: Using findstr /v on a string enclosed by quotation marks

#7 Post by gunitinug » 19 Nov 2017 17:05

I got it

Code: Select all

C:\Users\CMY\Desktop\test batch scripts\findstr\v switch>type persons.txt | findstr /v "\^"
person1
person2

C:\Users\CMY\Desktop\test batch scripts\findstr\v switch>type persons.txt | findstr /r "\^"
person1^
person2^
Used \ to escape ^ character........ on the link it used ^^ which didn't work in this case.
Last edited by gunitinug on 19 Nov 2017 19:48, edited 1 time in total.

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Using findstr /v on a string enclosed by quotation marks

#8 Post by Squashman » 19 Nov 2017 19:02

As you were told by Penpen, the caret is a special character with the findstr command and again if you read the help for the command it literally tells you to use the \ to escape characters in your search string.

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

Re: Using findstr /v on a string enclosed by quotation marks

#9 Post by pieh-ejdsch » 21 Nov 2017 06:00

Normally it is to use:

Code: Select all

findstr /veLc:"^"

Post Reply