简体   繁体   中英

how to write a script to read double quotes in powershell

Hi every one i am trying to read a string which is enclosed in double quotes within a file eg "cars" : I am using this command but unable to read it may be I am not using the right format to read the quotes

$houseid = $json |Get-Content |Select-String -pattern ""houseid":"| measure | select -exp count

write-host "houseid = " $houseid 

I am reading the file from json file

Wrap you pattern in single quotes when it contains quotes. Although it's not necessary, you should add -SimpleMatch to specify that it is a normal string-pattern and not regex.

#Valid sample
'"houseid":myjsonvalue' | Select-String -Pattern '"houseid":' -SimpleMatch | measure | select -ExpandProperty count    
1

#Invalid sample
'houseid:myjsonvalue' | Select-String -Pattern '"houseid":' -SimpleMatch | measure | select -ExpandProperty count
0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM