-2

I have a file with multiple lines as shown in the example below, but I need the chars mentioned in the pattern to be present in the output, in other words non-greedy match.

I need a way to remove the first part of the line and get the line which contains with starting with '07' or '08' and everything in the line till end of line.

Example:

  • Input: 'SELECT TZ_OFFSET (SESSIONTIMEZONE) FROM DUAL07.16.0162 (B0375, U0847)'
    • output: 07.16.0162 (B0375, U0207)
  • Input: 'SELECT TZ_OFFSET (SESSIONTIMEZONE) FROM DUAL08.15.0162 (B03075, U07)'
    • output: 08.15.0162 (B08075, U07)
Priya
  • 1
  • 1
  • 1
    Non-greedy does not mean chars are not added to the output. You are confusing notions here. What is your current code? What does not work? – Wiktor Stribiżew Oct 31 '18 at 13:12
  • Non greedy is ideally I would like to remove all chars before '07' but include 07 in the output string. So what I tried was ` inputStr -replace '^.*?07(.*)$','$1' ` but the output from that '.16.0162 (B0375, U0847)' and expected output is '07.16.0162 (B0375, U0847)' – Priya Nov 01 '18 at 05:27

2 Answers2

1

This should work:

$str = 'SELECT TZ_OFFSET (SESSIONTIMEZONE) FROM DUAL07.16.0162 (B0375, U0847)' 
$str -replace '^.*?DUAL(.*)$','$1'
TobyU
  • 3,718
  • 2
  • 21
  • 32
  • Yup, this is what I was looking for. file = "c:\abc.txt" (Get-Content -Path $file) -replace '^.*?DUAL(.*)$','$1'|set-content $file Thanks! – Priya Oct 31 '18 at 18:26
0
New-Variable -Name file -Value "c:\abc.txt" #This file contains the line that needs to be edited
(Get-Content -Path $file) -replace '^.*?DUAL(.*)$','$1'|set-content $file
Priya
  • 1
  • 1
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – stop-cran Nov 01 '18 at 14:47