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

Viewing 4 reply threads
  • 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: 512
      Points: 1,310
      Helping Hand
      Rank: Community Hero

      You can use -match to get these values 🙂

      # with -match
      $FileContents |
          Where-Object { $_ -match 'class (?<ClassName>\w+)' } |
          ForEach-Object { $matches['ClassName'] }
    • #172366
      Senior Moderator
      Topics: 8
      Replies: 1164
      Points: 4,076
      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 '.*?(?<Method>(\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: 1164
      Points: 4,076
      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]}
        }
Viewing 4 reply threads
  • The topic ‘How to trim a word from a sentence using powershell’ is closed to new replies.