How to trim a word from a sentence using powershell

Welcome Forums General PowerShell Q&A How to trim a word from a sentence using powershell

This topic contains 5 replies, has 3 voices, and was last updated by

 
Participant
4 weeks, 1 day ago.

  • Author
    Posts
  • #172327

    Participant
    Topics: 4
    Replies: 9
    Points: 28
    Rank: Member

    I'm having a set of lines like below:

    Public class ABC extended version of line

    Public class MNO extended version of line

    I've 100s of lines like this.  I need only the word which is after class.

    In the above example its ABC and MNO.

    Can we use the trim concept here ? Or any other ideas are much appreciated.

  • #172336

    Participant
    Topics: 2
    Replies: 483
    Points: 1,151
    Helping Hand
    Rank: Community Hero

    You can use -match to get these values 🙂

    # with -match
    $FileContents |
        Where-Object { $_ -match 'class (?\w+)' } |
        ForEach-Object { $matches['ClassName'] }
  • #172366

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,439
    Helping Hand
    Rank: Community Hero

    Another one using switch statement.

    switch -regex -file ($FileName) {
        'class (\w+)' {$matches[1]}
    }
    
  • #172372

    Participant
    Topics: 4
    Replies: 9
    Points: 28
    Rank: Member

    Thanks for your response 🙂

    My output looks like this :

    public class ABCD extends AbstractBshDefaultTaskPrep {

    public class EFGH extends somename {

    ..

    ..

    Here i want to have the output as public class ABCD and not as public class ABCD extends AbstractBshDefaultTaskPrep {

    This is code i am trying with is this .. Where can i use the switch or the trim concept or any other concept in the below code to get the desired output.

    function Find_ClassAndItsMethods { 
    $List = get-childitem "C:\Programs\data\Git\tc-pdm\src\Customization\Source" -recurse -filter "*.java"
    $Results = foreach ($file in $list) {
    $Text = Get-Content -Path $file.FullName
    $Classes = $Test | Where-Object {$_ -like '* class*'} 
    $Methods = $Text -match '\w+\([^\)]*\)' -ireplace '.*?(?(\w+ \w+|\w+\.\w+)\([^\)]*\)).*','${Method}'
    [PSCustomObject]@{
    File_Name = $file.FullName
    Class_Name = $Classes -join '; '
    Methods = $Methods -join '; '
    }
    }
    $Results | Export-Csv -Path C:\Programs\export.csv -NoTypeInformation
    }
    Find_ClassAndItsMethods
  • #172381

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,439
    Helping Hand
    Rank: Community Hero

    Did you try the examples provided above in previous replies ? is the output shown above by using those methods ?

    • #172399

      Participant
      Topics: 4
      Replies: 9
      Points: 28
      Rank: Member

      Yes , I did try with your example and its working absolutely fine .. Thank you for that ...

      But i also need for abstract class and final class :

      The below snippet is displaying only for the "class" . I need for "abstract class" and "final class" as well . How can we modify the below snippet for this requirement .

      switch -regex -file ($FileName) {
      'class (\w+)' {$matches[1]}
      }

You must be logged in to reply to this topic.