Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - regex - search - replace question

Reply
 
Old 10-11-2007   #1 (permalink)
Bin


 
 

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
Old 10-11-2007   #2 (permalink)
Marco Shaw [MVP]


 
 

Re: regex - search - replace question

Bin wrote:
Quote:

> 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
Old 10-11-2007   #3 (permalink)
Shay Levi


 
 

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


Quote:

> 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
Old 10-11-2007   #4 (permalink)
Marco Shaw [MVP]


 
 

Re: regex - search - replace question

Quote:
Quote:

>> 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
Old 10-12-2007   #5 (permalink)
Kiron


 
 

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
Old 10-12-2007   #6 (permalink)
Jeff


 
 

Re: regex - search - replace question

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

> 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
Reply

Thread Tools


Similar Threads
Thread Forum
regex replace function VB Script
Replace Start Menu Search with 3rd Party Search General Discussion
Re: Call to Regex.Replace is killing all CRLFs PowerShell
Search and replace - regex problem PowerShell
Where are things like -replace and split([regex} documented? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46