![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 > 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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; 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
| | #6 (permalink) |
| | 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 ${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 Specs![]() |
![]() |
| 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 | |||