# How to select Substring

**URL:** <https://forums.powershell.org/t/how-to-select-substring/5960>\
**Category:** PowerShell Help\
**Created:** [March 9, 2016, 6:34am UTC](https://forums.powershell.org/t/how-to-select-substring/5960 "2016-03-09T06:34:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vishal-joshi](https://avatars.discourse-cdn.com/v4/letter/v/fbc32d/32.png) [@vishal-joshi](https://forums.powershell.org/u/vishal-joshi)\
**Post date:** [March 9, 2016, 6:34am UTC](https://forums.powershell.org/t/how-to-select-substring/5960/1 "2016-03-09T06:34:37Z")

</div>

Hi All,  
I was trying to selecting string using a pattern “error”, So i am getting two different errors like below.

1. Desktop\DWH\datawarehouse.log:260:2016-03-01 01:41:24,077 [DwhTaskQueue] ERROR dwh  
:316 - Error in SQL: Sql[sqlString=delete from “P\_CONTROLMEASURE” where “CONTROLME  
ASUREID” in (select “CONTROLMEASUREID” from “T\_CONTROLMEASURE” where “AUDITID”=?),  
sqlTypes=[2]]  
Desktop\DWH\datawarehouse.log:262: at com.microsoft.sqlserver.jdbc.SQLServerExc  
eption.makeFromDatabaseError(SQLServerException.java:216)

2. Desktop\DWH\datawarehouse.log:549:2016-03-01 11:00:22,038 [DwhTaskQueue] ERROR dwh  
:326 - Connect failed: The driver could not establish a secure connection to SQL S  
erver by using Secure Sockets Layer (SSL) encryption. Error: “Read timed out Clien  
tConnectionId:6d43ba4a-ab04-430d-a966-0f08a1479079”.

I wanted to select all errors in file by searching with “error” string except some of the errors

For example select-string -pattern “error” it will get all errors in file but i dont want to select all errors. the result is above two error but i want to select this error and ignore another one

Desktop\DWH\datawarehouse.log:549:2016-03-01 11:00:22,038 [DwhTaskQueue] ERROR dwh  
:326 - Connect failed: The driver could not establish a secure connection to SQL S  
erver by using Secure Sockets Layer (SSL) encryption. Error: “Read timed out Clien  
tConnectionId:6d43ba4a-ab04-430d-a966-0f08a1479079”.

Please help me how can i do this.

---

<div class="post-metadata">

**Author:** ![random-commandline](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@random-commandline](https://forums.powershell.org/u/random-commandline)\
**Post date:** [March 9, 2016, 7:41am UTC](https://forums.powershell.org/t/how-to-select-substring/5960/2 "2016-03-09T07:41:20Z")

</div>

A switch statement will check each line of your log file.

```
# Display lines containing 'connect failed'
$file = Get-ChildItem -Path '.\Desktop\DWH\datawarehouse.log'
switch -Regex -File $file
{'connect failed' {$_}}
```

---

<div class="post-metadata">

**Author:** ![vishal-joshi](https://avatars.discourse-cdn.com/v4/letter/v/fbc32d/32.png) [@vishal-joshi](https://forums.powershell.org/u/vishal-joshi)\
**Post date:** [March 9, 2016, 9:03am UTC](https://forums.powershell.org/t/how-to-select-substring/5960/3 "2016-03-09T09:03:18Z")

</div>

I am gettinf same output by running this script  
$file = Get-ChildItem -Path ‘C:\Users\Username\Desktop\folder\datawarehouse.log’|sort {$_.LastWriteTime} | select -last 1  
switch -Regex -File $file  
{‘connect failed’ {$_}}

and  
this script  
$LogError = gci C:\Users\username\Desktop\DWH\datawarehouse.log | sort {$\_.LastWriteTime} | select -last 1 | select-string -pattern “ERROR”

Output:

[DwhTaskQueue] ERROR dwh:316 - Error in SQL: Sql[sqlString  
=delete from “P\_CONTROLMEASURE” where “CONTROLMEASUREID” in (select “CONTROLMEASUR  
EID” from “T\_CONTROLMEASURE” where “AUDITID”=?), sqlTypes=[2]]  
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServe  
rException.java:216)

[DwhTaskQueue] ERROR dwh:316 - Error in SQL: Sql[sqlString  
=delete from “P\_CONTROLMEASURE” where “CONTROLMEASUREID” in (select “CONTROLMEASUR  
EID” from “T\_CONTROLMEASURE” where “AUDITID”=?), sqlTypes=[2]]  
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServe  
rException.java:216)

[DwhTaskQueue] ERROR dwh:326 - Connect failed: The driver  
could not establish a secure connection to SQL Server by using Secure Sockets Laye  
r (SSL) encryption. Error: “Read timed out ClientConnectionId:6d43ba4a-ab04-430d-a  
966-0f08a1479079”.

In this file there are three error in this case i need to select only one that is “connect failed error” and want to ignore other one.  
i want to search an error with error string so that wherever error comes in file it will find that.

Please help

---

<div class="post-metadata">

**Author:** ![random-commandline](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@random-commandline](https://forums.powershell.org/u/random-commandline)\
**Post date:** [March 9, 2016, 12:21pm UTC](https://forums.powershell.org/t/how-to-select-substring/5960/4 "2016-03-09T12:21:17Z")

</div>

If each error is on a separate line, the switch statement should work. It should match any line containing the string ‘connect failed.’ Also, your post has only one log file, you do not have to use sort-object or select-object.

```
# Example
$logfile = @"
[DwhTaskQueue] ERROR dwh:316 – Error in SQL: Sql[sqlString=delete from "P_CONTROLMEASURE" where "CONTROLMEASUREID" in (select "CONTROLMEASUREID" from "T_CONTROLMEASURE" where "AUDITID"=?), sqlTypes=[2]]at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
[DwhTaskQueue] ERROR dwh:316 – Error in SQL: Sql[sqlString=delete from "P_CONTROLMEASURE" where "CONTROLMEASUREID" in (select "CONTROLMEASUREID" from "T_CONTROLMEASURE" where "AUDITID"=?), sqlTypes=[2]]at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
[DwhTaskQueue] ERROR dwh:326 – Connect failed: The drivercould not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "Read timed out ClientConnectionId:6d43ba4a-ab04-430d-a966-0f08a1479079".
"@ -split "`n"

$logfile -match 'connect failed'
# Results
# [DwhTaskQueue] ERROR dwh:326 – Connect failed: The drivercould not establish a #secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: # "Read timed out ClientConnectionId:6d43ba4a-ab04-430d-a966-0f08a1479079".#>
```

---

<div class="post-metadata">

**Author:** ![vishal-joshi](https://avatars.discourse-cdn.com/v4/letter/v/fbc32d/32.png) [@vishal-joshi](https://forums.powershell.org/u/vishal-joshi)\
**Post date:** [March 10, 2016, 8:39am UTC](https://forums.powershell.org/t/how-to-select-substring/5960/5 "2016-03-10T08:39:09Z")

</div>

Thnaks

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:42pm UTC](https://forums.powershell.org/t/how-to-select-substring/5960/6 "2024-05-16T20:42:42Z")

</div>


