Removing the SPACE before the second occurring of underscore "_"

Welcome Forums General PowerShell Q&A Removing the SPACE before the second occurring of underscore "_"

This topic contains 12 replies, has 4 voices, and was last updated by

 
Participant
3 months ago.

  • Author
    Posts
  • #161904

    Participant
    Topics: 2
    Replies: 5
    Points: 10
    Rank: Member

    Hello There:

    Greetings all!

    Requesting you to help with this specific request:

    Would like to remove the space just before the second occurrence of "_" and rename the file using the PowerShell

    For Example: 221231_John Reeves R _JoiningLetter.pdf  to 221231_John Reeves R_JoiningLetter.pdf

    and using this cmdlet to list those pdf's which are having space just before the second instance of "_"

    gci . -recurse -filter *.pdf| where {$_.basename -match "(^(\d{8})(_)([a-zA-Z ]*)( )(_))" }

    and please note, we cant suppress all spaces in the filename as the second string which contains employee name will contain whitespaces.

    Appreciated!

    Cheers,

     

     

     

     

     

     

     

     

     

  • #161951

    Participant
    Topics: 1
    Replies: 1530
    Points: 2,591
    Helping Hand
    Rank: Community Hero

    No big deal ... simply replace all white spaces followed by an underline ...

    '221231_John Reeves R _JoiningLetter.pdf' -replace '\s+(?=_)'
    • #161954

      Participant
      Topics: 2
      Replies: 5
      Points: 10
      Rank: Member

      Olaf: thanks for the response...

       

      However as I noted, we can't suppress all spaces but only one before the second occurrence of the "_" .....

  • #162003

    Participant
    Topics: 1
    Replies: 1530
    Points: 2,591
    Helping Hand
    Rank: Community Hero

    Are there more than one space before an underline charachter? ... did you try the code snippet? ... it removes ONLY the space befor the underline. The other spaces remain in the string.

  • #162006

    Participant
    Topics: 12
    Replies: 232
    Points: 466
    Helping Hand
    Rank: Contributor

    Should work as Olaf says.
    But if you have more than one space or need to match an exact number of spaces before the underscore you can add a quantifier.
    E.g. '\s{1}(?=_)'

    But that seem to be pretty strange filename requirements if you ask me 🙂

    A tip is to use a regex tester, there are several online that simplifies regex testing.
    E.g.
    https://regex101.com/

  • #162015

    Participant
    Topics: 1
    Replies: 1530
    Points: 2,591
    Helping Hand
    Rank: Community Hero

    Actually I meant "Are there more than one occurence of a space in front of an underline charachter?" 😉

    • #162131

      Participant
      Topics: 2
      Replies: 5
      Points: 10
      Rank: Member

      Olaf – no chance of double space there as I have another cmdlet which suppresses the double whitespace at a position.

      Fredrik – yes its the archived data for the last decade and am assisting to set things right ..phase by phase

      and i have used "https://rubular.com/" for coming to a conclusion of finding those files – using regex, now got stuck with the rename options within powershell

      Cheers...

       

       

  • #162017

    Participant
    Topics: 8
    Replies: 17
    Points: 127
    Rank: Participant

    I strongly support Fredrik's suggestion of Regex101. It was a lifesaver for developing Luhn Algorithms.

    Throw the regex in, then throw in your sample filename, and play with it until you match what you need.

    I also strongly suggest checking out https://www.rexegg.com/ for detailed explanations of some of the concepts in RegEx.

    • #162132

      Participant
      Topics: 2
      Replies: 5
      Points: 10
      Rank: Member

      thanks Charles for the URL..... Noted

  • #162135

    Participant
    Topics: 2
    Replies: 5
    Points: 10
    Rank: Member

    Very similar to the above requested, I've resolved the space and renamed the file where it was after the first occurrence of Underscore "_"

    For Example:

    12345678_ John Reeves R_JoiningLetter.PDF

    will be renamed as

    12345678_John Reeves R_JoiningLetter.PDF

    using::

    gci . -recurse -filter *.pdf | where {$_.basename -match "(^(\d{8})(_)(( )[a-zA-Z ]*))"} | rename-item -newname { $_.Name -replace '(.{9}).(.+)','$1$2' }

    In these cases the position is fixed that is 10th position of the string and so removing space then renaming the file is pretty straightforward.

    So the assistance required here is for the space before the second instance of underscore "_"

    This is the logic I came up with for the resolving the cases of have the space just before the "Underscore" , if any better logic can be implemented, am open for them...

    Kindly advise!

    Cheers,

     

     

  • #162137

    Participant
    Topics: 1
    Replies: 1530
    Points: 2,591
    Helping Hand
    Rank: Community Hero

    I really do not understand what's wrong with my suggestions. The code snippet I posted matches ONLY spaces occuring in front of an underline – NO OTHER OCCURENCES OF ANY WHITE SPACE CHARACHTERS. 😉

    Did you try it at least?

    • #162140

      Participant
      Topics: 2
      Replies: 5
      Points: 10
      Rank: Member

      Olaf —- Yes yes its worked , sorry i missed out the snippet early...

       

      Worked brilliantly ....

      Cheers ......

       

  • #162159

    Participant
    Topics: 1
    Replies: 1530
    Points: 2,591
    Helping Hand
    Rank: Community Hero

    Thank you. Now I can sleep peacefully again. 😉 😀

The topic ‘Removing the SPACE before the second occurring of underscore "_"’ is closed to new replies.