Windows Vista Forums

regex - search - replace question
  1. #1


    Bin Guest

    regex - search - replace question

    Hi,

    I have and input text file containing data in the following format:

    9.00; 1.00-; .00000;00; 25.00; 672;SC;006700014414841;107/09/14;
    19.00; 22.00-; .00000;00; 205.00;
    672;SC;006700014414841;107/09/14;

    Is there any easy way to shift the negative sign (-) in the second column to
    left? Output text should be like this:

    9.00; -1.00; .00000;00; 25.00; 672;SC;006700014414841;107/09/14;
    19.00; -22.00; .00000;00; 205.00;
    672;SC;006700014414841;107/09/14;

    Is there any easy way using regex or any search-replace technique?

    Any help much appreciated.

    Thanks,
    PN




      My System SpecsSystem Spec

  2. #2


    Marco Shaw [MVP] Guest

    Re: regex - search - replace question

    Bin wrote:

    > Hi,
    >
    > I have and input text file containing data in the following format:
    >
    > 9.00; 1.00-; .00000;00; 25.00; 672;SC;006700014414841;107/09/14;
    > 19.00; 22.00-; .00000;00; 205.00;
    > 672;SC;006700014414841;107/09/14;
    >
    > Is there any easy way to shift the negative sign (-) in the second column to
    > left? Output text should be like this:
    >
    > 9.00; -1.00; .00000;00; 25.00; 672;SC;006700014414841;107/09/14;
    > 19.00; -22.00; .00000;00; 205.00;
    > 672;SC;006700014414841;107/09/14;
    >
    > Is there any easy way using regex or any search-replace technique?
    >
    > Any help much appreciated.
    >
    > Thanks,
    > PN
    >
    Easy enough... What do you want to do with the result? Create a 2nd
    file with it? Overwrite the original? I'm assuming this is
    line-per-line, and the general format doesn't change?

    I've got something, but it isn't pretty yet...

    PSH> $string
    9.00; 1.00-; .00000;00; 25.00;
    672;SC;006700014414841;107/09/14;

    PSH> $string|`
    foreach-object{$split=$split=$string.split(";")
    $split[0]+";-"+$_.split(";")[1].trimend("-1").trimstart()}
    9.00;-1.00

    Ain't pretty, and I loose some of the spacing. A regex would be neater.

    How do the original spaces fit into all of this? Do they need to be
    preserved?

    Marco

    --
    Microsoft MVP - Windows PowerShell
    http://www.microsoft.com/mvp

    PowerGadgets MVP
    http://www.powergadgets.com/mvp

    Blog:
    http://marcoshaw.blogspot.com

      My System SpecsSystem Spec

  3. #3


    Shay Levi Guest

    Re: regex - search - replace question

    Mine is no pretty too, and there are still open questions as Marco wrote

    $text="9.00; 1.00-; .00000;00; 25.00; 672;SC;006700014414841;107/09/14; 19.00;
    22.00-; .00000;00; 205.00; 672;SC;006700014414841;107/09/14;"
    $text.split(";") | foreach {$_=$_.trim(); if(($_[-1]) -eq "-"){ $_= ("-"+$_.replace("-",""))
    }$_}

    9.00
    -1.00
    ..00000
    00
    25.00
    672
    SC
    006700014414841
    107/09/14
    19.00
    -22.00
    ..00000
    00
    205.00
    672
    SC
    006700014414841
    107/09/14

    Regex is definitely a better solution.


    Shay
    http://scriptolog.blogspot.com



    > Hi,
    >
    > I have and input text file containing data in the following format:
    >
    > 9.00; 1.00-; .00000;00; 25.00;
    > 672;SC;006700014414841;107/09/14; 19.00; 22.00-; .00000;00;
    > 205.00; 672;SC;006700014414841;107/09/14;
    >
    > Is there any easy way to shift the negative sign (-) in the second
    > column to left? Output text should be like this:
    >
    > 9.00; -1.00; .00000;00; 25.00;
    > 672;SC;006700014414841;107/09/14; 19.00; -22.00; .00000;00;
    > 205.00; 672;SC;006700014414841;107/09/14;
    >
    > Is there any easy way using regex or any search-replace technique?
    >
    > Any help much appreciated.
    >
    > Thanks,
    > PN



      My System SpecsSystem Spec

  4. #4


    Marco Shaw [MVP] Guest

    Re: regex - search - replace question


    >> Is there any easy way to shift the negative sign (-) in the second
    >> column to left? Output text should be like this:
    >>
    >> 9.00; -1.00; .00000;00; 25.00;
    >> 672;SC;006700014414841;107/09/14;
    >> 19.00; -22.00; .00000;00; 205.00;
    >> 672;SC;006700014414841;107/09/14;
    Here's one way:

    PSH>get-content data.txt|foreach-object{$_ -replace "(\d*\D*\d*)-;",'-$1;'}
    9.00; -1.00; .00000;00; 25.00;
    672;SC;006700014414841;107/09/14;
    19.00; -22.00; .00000;00; 205.00;
    672;SC;006700014414841;107/09/14;

    (Pipe to out-file to save the output to a new file.)

    (Thanks Lee Holmes for all the details you put in your books.)

    Marco

      My System SpecsSystem Spec

  5. #5


    Kiron Guest

    Re: regex - search - replace question

    # output to screen
    (get-content file.txt) -replace '(\d+\.\d+)-', '-$1'

    # output to new file
    set-content newFile.txt ((get-content file.txt) -replace '(\d+\.\d+)-', '-$1')

    --
    Kiron

      My System SpecsSystem Spec

  6. #6


    Jeff Guest

    Re: regex - search - replace question

    On Oct 12, 6:15 am, Bin <B...@xxxxxx> wrote:

    > Hi,
    >
    > I have and input text file containing data in the following format:
    >
    > 9.00; 1.00-; .00000;00; 25.00; 672;SC;006700014414841;107/09/14;
    > 19.00; 22.00-; .00000;00; 205.00;
    > 672;SC;006700014414841;107/09/14;
    >
    > Is there any easy way to shift the negative sign (-) in the second column to
    > left? Output text should be like this:
    >
    > 9.00; -1.00; .00000;00; 25.00; 672;SC;006700014414841;107/09/14;
    > 19.00; -22.00; .00000;00; 205.00;
    > 672;SC;006700014414841;107/09/14;
    >
    > Is there any easy way using regex or any search-replace technique?
    >
    > Any help much appreciated.
    >
    > Thanks,
    > PN
    One more, just for completeness. Update the file "in place":

    ${C:file.txt} = ${C:file.txt} -replace "(\d+\.\d+)-", "-$1"

    This assumes the file is in the current working directory on the C:
    drive.

    Jeff


      My System SpecsSystem Spec

regex - search - replace question problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
regex replace function James VB Script 1 18 Aug 2009
Replace Start Menu Search with 3rd Party Search RTAdams89 General Discussion 0 02 Jun 2009
Re: Call to Regex.Replace is killing all CRLFs Kiron PowerShell 1 02 Feb 2009
Search and replace - regex problem dandbnews@talktalk.net PowerShell 7 21 Jun 2007
Where are things like -replace and split([regex} documented? =?Utf-8?B?RXJzdHdoaWxlSUlJ?= PowerShell 2 25 Jul 2006