-1

In my text file there are some strings which I want to exclude using powershell. Sample of my text file looks like this

"DEFINE QLOCAL *ALTDATE(2015-02-19) *ALTTIME(10.38.13) *CRDATE(2015-02-19) *CRTIME(10.38.13) DISTL(NO) MAXDEPTH(999999999) MAXMSGL(32762) REPLACE DEFINE QMODEL('SYSTEM.JMS.TEMPQ.MODEL') *ALTDATE(2015-02-19) *ALTTIME(10.38.14) *CRDATE(2015-02-19) *CRTIME(10.38.14) DEFTYPE(TEMPDYN) DISTL(NO) MAXDEPTH(5000) MAXMSGL(104857600) *ALTDATE(2015-02-19)".

The strings which I want remove from the file all start with asterisks(*) like *CRDATE(12-45-1245) *CRTIME(12.12.12)... The question is how I'm going to remove from the file using powershell and what kind of command I have to use?. I have done this on linux using stream editor (SED). For instance sed -e 's/.CURDEPTH.[0-9]*)//' -e 's/.IPPROCS.[0-9]*)//' But I don't know the equivalent for windows powershell. Any suggestions can help. Thanks

SomethingDark
  • 13,229
  • 5
  • 50
  • 55
Abdulkadir
  • 11
  • 1
  • 5

2 Answers2

2

The issue you have is probably going to run into multiple roadblocks because you're going to want to do a RegEx replace, which it looks like you may have done in Linux before where you realized it or not, and you're going to want to read a file into PowerShell in it's entirety because your strings look like they may span multiple lines.

I'm not going to teach you RegEx here, for the details of how it works take a look at www.regular-expressions.info. What I will help with is a RegEx that will find what you're looking for, and help you read your file in such a way that you can get all of the matches removed.

Let's start with reading in your file. The Get-Content cmdlet will read the file, but by default it break it into an array of strings, each string being one line. What you will want to do is use the -Raw parameter with it so that it reads the entire file into one string. Something like this:

Get-Content "C:\Path\To\File.txt" -Raw

Then you can enclose that in parenthesis so that it finishes that command, and do a -Replace on it which will perform a RegEx search and replace function. The RegEx search:

"\*\S*"

Should work for you. That looks for any asteriscs and as many non-whitespace characters that follow it as possible until the next whitespace. You can see how it works at:

https://regex101.com/r/bE9lP7/1

Then just pipe that to a Set-Content cmdlet and you should be set. So the code should look like:

(Get-Content "C:\Path\To\File.txt" -Raw) -Replace "\*\S*" | Set-Content "C:\Path\To\NewFile.txt"
Dharman
  • 30,962
  • 25
  • 85
  • 135
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
  • Thanks TheMadTech for quick response. Adding some more points just to make it clear. 1st..ReGex "*\S*" is working perfect!. the problem I'm facing right now is I've 8 lines at top of my file which you may think of as a header and I don't want the ReGex "*\S*" to go through it. But how? ) 2nd.. after any string which starts with asterics is removed, it leaving a white space/tab. How I'm going to limit the spaces between carachters in to singel space or tab? – Abdulkadir Mar 03 '15 at 01:15
  • To avoid the double space change the `"\*\S"` to `"\*\S "` by adding a space at the end of the string. As for maintaining the first 8 lines, that can get more complicated. Do you know if any of the strings that you want to replace cross a line break? – TheMadTechnician Mar 03 '15 at 02:48
  • Whenever I run this command from Powershell terminal it's working fine and I'm getting the output the way I want but when I save this command to batch file(becouse I want to add to Windows task schedualer) I'm getting "-Replace was unexpected at this time."...any help please... (Get-Content "C:\TO\INPUTFILE.txt" ) -Replace "\*ALTTIME\S* |\*ALTDATE\S* |\*CRTIME\S* |\*CRDATE\S* |\*IPPROCS\S* |\*OPPROCS\S*" | Set-Content "C:\TO\OUTPUTFILE.txt" – Abdulkadir Mar 03 '15 at 17:55
  • Save it as a .ps1 file and run the PowerShell script from the Task Scheduler. Look at [this SO question](http://stackoverflow.com/questions/23953926/how-to-execute-a-powershell-script-automatically-using-windows-task-scheduler) for info on how to do that. – TheMadTechnician Mar 03 '15 at 18:55
0
cls
$str = "DEFINE QLOCAL *ALTDATE(2015-02-19) *ALTTIME(10.38.13) *CRDATE(2015-02-19) *CRTIME(10.38.13) DISTL(NO) MAXDEPTH(999999999) MAXMSGL(32762) REPLACE DEFINE QMODEL('SYSTEM.JMS.TEMPQ.MODEL') *ALTDATE(2015-02-19) *ALTTIME(10.38.14) *CRDATE(2015-02-19) *CRTIME(10.38.14) DEFTYPE(TEMPDYN) DISTL(NO) MAXDEPTH(5000) MAXMSGL(104857600) *ALTDATE(2015-02-19)"
$str = [regex]::replace($str, '\*.*?\(.*?\)\s', '')
$str 
JohnLBevan
  • 22,735
  • 13
  • 96
  • 178