![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | Clear-Content Cmdlet and Processing Lines in Text File I would like to use PowerShell to remove lines in a Text file that is formatted with several columns. I would like to remove lines in the file up to where the column value for Mode equals a particular string value. Is is possible to do this with Clear-Content? Thanks. |
My System Specs![]() |
| | #2 (permalink) |
| | RE: Clear-Content Cmdlet and Processing Lines in Text File Let me be a little more specific: the file header looks like this: Temp Power Impedance Mode 23 34 345 Initial ............... .............. 34 45 2345 Measure 35 36 4545 Treatment .............. ............. I would like, if possible, to use Clear-content on each line in this file where the Mode column != Treatment. Is this possible? Thanks. "Eudaimonia" wrote: Quote: > I would like to use PowerShell to remove lines in a Text file that is > formatted with several columns. I would like to remove lines in the file up > to where the column value for Mode equals a particular string value. > > Is is possible to do this with Clear-Content? > > Thanks. |
My System Specs![]() |
| | #3 (permalink) |
| | RE: Clear-Content Cmdlet and Processing Lines in Text File No Clear-content removes all of the content in the file You will have to do a get-content and then check for the values you want to keep and then write out to a new file -- Richard Siddaway Please note that all scripts are supplied "as is" and with no warranty Blog: http://richardsiddaway.spaces.live.com/ PowerShell User Group: http://www.get-psuguk.org.uk "Eudaimonia" wrote: Quote: > I would like to use PowerShell to remove lines in a Text file that is > formatted with several columns. I would like to remove lines in the file up > to where the column value for Mode equals a particular string value. > > Is is possible to do this with Clear-Content? > > Thanks. |
My System Specs![]() |
| | #4 (permalink) |
| | RE: Clear-Content Cmdlet and Processing Lines in Text File Hi Eudaimonia, Clear-content cmdlet is designed to clear ALL file content. It is better to choose the Import-Csv cmdlet and filter out the entries you don't need, then save the results to a new file with Export-Csv. You're file is delimited with a space delimiter and Import-Csv gets only comma seperated files, so you'll need to replace all spaces with a comma. # read the content of the current file $temp = Get-Content yourfilePath.csv # replace spaces with commas and write to a new file $temp = $temp -replace " ","," $temp | Out-File newCsvFile.csv -encoding ASCII # import the new file $csv = Import-csv newCsvFile.csv # filter and save to a new csv file $csv | where {$_.Mode -eq "something"} | export-csv c:\final.csv ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic Quote: > Let me be a little more specific: > the file header looks like this: > Temp Power Impedance Mode > 23 34 345 Initial > .............. > ............. > 34 45 2345 Measure > 35 36 4545 Treatment > ............. > ............ > I would like, if possible, to use Clear-content on each line in this > file where the Mode column != Treatment. Is this possible? > > Thanks. > > "Eudaimonia" wrote: > Quote: >> I would like to use PowerShell to remove lines in a Text file that is >> formatted with several columns. I would like to remove lines in the >> file up to where the column value for Mode equals a particular string >> value. >> >> Is is possible to do this with Clear-Content? >> >> Thanks. >> |
My System Specs![]() |
| | #5 (permalink) |
| | RE: Clear-Content Cmdlet and Processing Lines in Text File Thanks! This is really useful. My example could be clearer; the formatting is comma delimited. Also, I now realize that what I am trying to do is remove all the lines at the beginning of the file up to but NOT including where the Mode is equal to TREATMENT. So, essentially, for each line where mode does not equal treatment, delete the line up to the first occurence of the line where the mode column equals TREATMENT. The pattern you listed below of $csv | where {$_.Mode..... is like a grep where it will cast the results to a boolean type (if true, pass the object to the pipeline, otherwise, don't). What I need is a routine that will end once it encounters the Mode = TREATMENT, kinda like a while....wend in VB while the Mode != TREATMENT, delete line. Once it encounters the first TREATMENT, the routine ends. Thanks again for the help and sorry for the confusion. "Shay Levi" wrote: Quote: > Hi Eudaimonia, > > Clear-content cmdlet is designed to clear ALL file content. > It is better to choose the Import-Csv cmdlet and filter out the entries you > don't need, then save the results to a new file with Export-Csv. > You're file is delimited with a space delimiter and Import-Csv gets only > comma seperated files, so you'll need to replace all spaces with a comma. > > > # read the content of the current file > $temp = Get-Content yourfilePath.csv > > # replace spaces with commas and write to a new file > $temp = $temp -replace " ","," > $temp | Out-File newCsvFile.csv -encoding ASCII > > # import the new file > $csv = Import-csv newCsvFile.csv > > # filter and save to a new csv file > $csv | where {$_.Mode -eq "something"} | export-csv c:\final.csv > > > > ----- > Shay Levi > $cript Fanatic > http://scriptolog.blogspot.com > Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic > > > Quote: > > Let me be a little more specific: > > the file header looks like this: > > Temp Power Impedance Mode > > 23 34 345 Initial > > .............. > > ............. > > 34 45 2345 Measure > > 35 36 4545 Treatment > > ............. > > ............ > > I would like, if possible, to use Clear-content on each line in this > > file where the Mode column != Treatment. Is this possible? > > > > Thanks. > > > > "Eudaimonia" wrote: > > Quote: > >> I would like to use PowerShell to remove lines in a Text file that is > >> formatted with several columns. I would like to remove lines in the > >> file up to where the column value for Mode equals a particular string > >> value. > >> > >> Is is possible to do this with Clear-Content? > >> > >> Thanks. > >> > > |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Clear-Content Cmdlet and Processing Lines in Text File Here are two options: 1- Since Get-Content returns an array of lines you can remove all lines --except the header-- that do not contain 'Treatment' like this: $content = @(gc testData.csv) $lastIndex = $content.length - 1 $targetIndex = @(0..$lastIndex | ? {$content[$_] -match 'Treatment'})[0] $content[0..0 + $targetIndex..$lastIndex] | Out-File newData1.csv -Encoding ASCII ipcsv newData1.csv 2- Set a flag to skip the first objects whose Mode value is not 'Treatment': $pass = $FALSE ipcsv testData.csv | % { if ($_.mode -eq 'Treatment') {$pass = $TRUE} if ($pass) {$_} } | epcsv newData2.csv -noType ipcsv newData2.csv -- Kiron |
My System Specs![]() |
| | #7 (permalink) |
| | RE: Clear-Content Cmdlet and Processing Lines in Text File Hey Eudaimonia, I'm sorry, but for once I think the solutions are overly complicated. Or I just haven't really understood what it is you're asking for ![]() However, I do think the already posted examples can be simplified a lot: $a = Get-Content c:\test\treatment.txt | where { $_ -match "treatment" } | Add-Content -path c:\test\results.txt It simply reads your file, all lines that has "treatment" in it gets added to a new file. Best Regards, Jacob Saaby Nielsen mailto:jacob.saaby@xxxxxx Quote: > Thanks! This is really useful. My example could be clearer; the > formatting is comma delimited. Also, I now realize that what I am > trying to do is remove all the lines at the beginning of the file up > to but NOT including where the Mode is equal to TREATMENT. So, > essentially, for each line where mode does not equal treatment, delete > the line up to the first occurence of the line where the mode column > equals TREATMENT. > > The pattern you listed below of $csv | where {$_.Mode..... is like a > grep where it will cast the results to a boolean type (if true, pass > the object to the pipeline, otherwise, don't). What I need is a > routine that will end once it encounters the Mode = TREATMENT, kinda > like a while....wend in VB while the Mode != TREATMENT, delete line. > Once it encounters the first TREATMENT, the routine ends. > > Thanks again for the help and sorry for the confusion. > > "Shay Levi" wrote: > Quote: >> Hi Eudaimonia, >> >> Clear-content cmdlet is designed to clear ALL file content. >> It is better to choose the Import-Csv cmdlet and filter out the >> entries you >> don't need, then save the results to a new file with Export-Csv. >> You're file is delimited with a space delimiter and Import-Csv gets >> only >> comma seperated files, so you'll need to replace all spaces with a >> comma. >> # read the content of the current file >> $temp = Get-Content yourfilePath.csv >> # replace spaces with commas and write to a new file >> $temp = $temp -replace " ","," >> $temp | Out-File newCsvFile.csv -encoding ASCII >> # import the new file >> $csv = Import-csv newCsvFile.csv >> # filter and save to a new csv file >> $csv | where {$_.Mode -eq "something"} | export-csv c:\final.csv >> ----- >> Shay Levi >> $cript Fanatic >> http://scriptolog.blogspot.com >> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic Quote: >>> Let me be a little more specific: >>> the file header looks like this: >>> Temp Power Impedance Mode >>> 23 34 345 Initial >>> .............. >>> ............. >>> 34 45 2345 Measure >>> 35 36 4545 Treatment >>> ............. >>> ............ >>> I would like, if possible, to use Clear-content on each line in this >>> file where the Mode column != Treatment. Is this possible? >>> Thanks. >>> >>> "Eudaimonia" wrote: >>> >>>> I would like to use PowerShell to remove lines in a Text file that >>>> is formatted with several columns. I would like to remove lines in >>>> the file up to where the column value for Mode equals a particular >>>> string value. >>>> >>>> Is is possible to do this with Clear-Content? >>>> >>>> Thanks. >>>> |
My System Specs![]() |
| | #8 (permalink) |
| | RE: Clear-Content Cmdlet and Processing Lines in Text File Hey Jacob, sorry, the line only needs to be this: Get-Content c:\test\treatment.txt | where { $_ -match "treatment" } | Add-Content -path c:\test\results.txt (Without the $a) Best Regards, Jacob Saaby Nielsen mailto:jacob.saaby@xxxxxx Quote: > Hey Eudaimonia, > > I'm sorry, but for once I think the solutions are overly complicated. > Or > I just haven't really > understood what it is you're asking for ![]() > However, I do think the already posted examples can be simplified a > lot: > > $a = Get-Content c:\test\treatment.txt | where { $_ -match "treatment" > } | Add-Content -path c:\test\results.txt > > It simply reads your file, all lines that has "treatment" in it gets > added to a new file. > > Best Regards, > Jacob Saaby Nielsen > mailto:jacob.saaby@xxxxxx Quote: >> Thanks! This is really useful. My example could be clearer; the >> formatting is comma delimited. Also, I now realize that what I am >> trying to do is remove all the lines at the beginning of the file up >> to but NOT including where the Mode is equal to TREATMENT. So, >> essentially, for each line where mode does not equal treatment, >> delete the line up to the first occurence of the line where the mode >> column equals TREATMENT. >> >> The pattern you listed below of $csv | where {$_.Mode..... is like a >> grep where it will cast the results to a boolean type (if true, pass >> the object to the pipeline, otherwise, don't). What I need is a >> routine that will end once it encounters the Mode = TREATMENT, kinda >> like a while....wend in VB while the Mode != TREATMENT, delete line. >> Once it encounters the first TREATMENT, the routine ends. >> >> Thanks again for the help and sorry for the confusion. >> >> "Shay Levi" wrote: >> Quote: >>> Hi Eudaimonia, >>> >>> Clear-content cmdlet is designed to clear ALL file content. >>> It is better to choose the Import-Csv cmdlet and filter out the >>> entries you >>> don't need, then save the results to a new file with Export-Csv. >>> You're file is delimited with a space delimiter and Import-Csv gets >>> only >>> comma seperated files, so you'll need to replace all spaces with a >>> comma. >>> # read the content of the current file >>> $temp = Get-Content yourfilePath.csv >>> # replace spaces with commas and write to a new file >>> $temp = $temp -replace " ","," >>> $temp | Out-File newCsvFile.csv -encoding ASCII >>> # import the new file >>> $csv = Import-csv newCsvFile.csv >>> # filter and save to a new csv file >>> $csv | where {$_.Mode -eq "something"} | export-csv c:\final.csv >>> ----- >>> Shay Levi >>> $cript Fanatic >>> http://scriptolog.blogspot.com >>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic >>>> Let me be a little more specific: >>>> the file header looks like this: >>>> Temp Power Impedance Mode >>>> 23 34 345 Initial >>>> .............. >>>> ............. >>>> 34 45 2345 Measure >>>> 35 36 4545 Treatment >>>> ............. >>>> ............ >>>> I would like, if possible, to use Clear-content on each line in >>>> this >>>> file where the Mode column != Treatment. Is this possible? >>>> Thanks. >>>> "Eudaimonia" wrote: >>>> >>>>> I would like to use PowerShell to remove lines in a Text file that >>>>> is formatted with several columns. I would like to remove lines >>>>> in the file up to where the column value for Mode equals a >>>>> particular string value. >>>>> >>>>> Is is possible to do this with Clear-Content? >>>>> >>>>> Thanks. >>>>> |
My System Specs![]() |
| | #9 (permalink) |
| | RE: Clear-Content Cmdlet and Processing Lines in Text File Hi Jacob, Sorry for not being clearer. Your script does work if all I want is to get the lines with TREATMENT; what I didn't describe clear enough is the problem that after the first occurence of TREATMENT in the file, I want to preserve all of the lines some of which have TREATMENT and some of which have other string values for the Mode column. What your script does is it takes only the TREATMENT lines. Thanks! "Jacob Saaby Nielsen" wrote: Quote: > Hey Jacob, > > sorry, the line only needs to be this: > > Get-Content c:\test\treatment.txt | where { $_ -match "treatment" } | Add-Content > -path c:\test\results.txt > > (Without the $a) > > Best Regards, > Jacob Saaby Nielsen > mailto:jacob.saaby@xxxxxx > Quote: > > Hey Eudaimonia, > > > > I'm sorry, but for once I think the solutions are overly complicated. > > Or > > I just haven't really > > understood what it is you're asking for ![]() > > However, I do think the already posted examples can be simplified a > > lot: > > > > $a = Get-Content c:\test\treatment.txt | where { $_ -match "treatment" > > } | Add-Content -path c:\test\results.txt > > > > It simply reads your file, all lines that has "treatment" in it gets > > added to a new file. > > > > Best Regards, > > Jacob Saaby Nielsen > > mailto:jacob.saaby@xxxxxx Quote: > >> Thanks! This is really useful. My example could be clearer; the > >> formatting is comma delimited. Also, I now realize that what I am > >> trying to do is remove all the lines at the beginning of the file up > >> to but NOT including where the Mode is equal to TREATMENT. So, > >> essentially, for each line where mode does not equal treatment, > >> delete the line up to the first occurence of the line where the mode > >> column equals TREATMENT. > >> > >> The pattern you listed below of $csv | where {$_.Mode..... is like a > >> grep where it will cast the results to a boolean type (if true, pass > >> the object to the pipeline, otherwise, don't). What I need is a > >> routine that will end once it encounters the Mode = TREATMENT, kinda > >> like a while....wend in VB while the Mode != TREATMENT, delete line. > >> Once it encounters the first TREATMENT, the routine ends. > >> > >> Thanks again for the help and sorry for the confusion. > >> > >> "Shay Levi" wrote: > >> > >>> Hi Eudaimonia, > >>> > >>> Clear-content cmdlet is designed to clear ALL file content. > >>> It is better to choose the Import-Csv cmdlet and filter out the > >>> entries you > >>> don't need, then save the results to a new file with Export-Csv. > >>> You're file is delimited with a space delimiter and Import-Csv gets > >>> only > >>> comma seperated files, so you'll need to replace all spaces with a > >>> comma. > >>> # read the content of the current file > >>> $temp = Get-Content yourfilePath.csv > >>> # replace spaces with commas and write to a new file > >>> $temp = $temp -replace " ","," > >>> $temp | Out-File newCsvFile.csv -encoding ASCII > >>> # import the new file > >>> $csv = Import-csv newCsvFile.csv > >>> # filter and save to a new csv file > >>> $csv | where {$_.Mode -eq "something"} | export-csv c:\final.csv > >>> ----- > >>> Shay Levi > >>> $cript Fanatic > >>> http://scriptolog.blogspot.com > >>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic > >>>> Let me be a little more specific: > >>>> the file header looks like this: > >>>> Temp Power Impedance Mode > >>>> 23 34 345 Initial > >>>> .............. > >>>> ............. > >>>> 34 45 2345 Measure > >>>> 35 36 4545 Treatment > >>>> ............. > >>>> ............ > >>>> I would like, if possible, to use Clear-content on each line in > >>>> this > >>>> file where the Mode column != Treatment. Is this possible? > >>>> Thanks. > >>>> "Eudaimonia" wrote: > >>>> > >>>>> I would like to use PowerShell to remove lines in a Text file that > >>>>> is formatted with several columns. I would like to remove lines > >>>>> in the file up to where the column value for Mode equals a > >>>>> particular string value. > >>>>> > >>>>> Is is possible to do this with Clear-Content? > >>>>> > >>>>> Thanks. > >>>>> > > |
My System Specs![]() |
| | #10 (permalink) |
| | RE: Clear-Content Cmdlet and Processing Lines in Text File Hi, The following will output all lines beginning with the first line containing the text Treatment. $foundit = $false Get-Content inputfile | Where {$foundit -or ($foundit = $_ -match 'Treatment')} /Fredrik "Eudaimonia" wrote: Quote: > Hi Jacob, > > Sorry for not being clearer. Your script does work if all I want is to get > the lines with TREATMENT; what I didn't describe clear enough is the problem > that after the first occurence of TREATMENT in the file, I want to preserve > all of the lines some of which have TREATMENT and some of which have other > string values for the Mode column. What your script does is it takes only > the TREATMENT lines. > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Using lines in a text file | PowerShell | |||
| Howto: Add lines of text from a specific point in a text file.. | VB Script | |||
| get the number of lines in a text file | PowerShell | |||
| Removing lines from a text file | PowerShell | |||
| removing first three lines in a text file | PowerShell | |||