2

I'm using the variable $listing as the file name as given below.

Get-Content BHS_output.txt | Select-String -pattern $listing| sort | Foreach-Object {$_ -replace "$listing", ""} > $listing

Where $listing is the variable from the first row of the file on first iteration. On the second iteration

$listing = Get-Content .\file.txt | select-object -First 1 . 

$listing must have BHS_E_CNBY20150622035126.CSV .. Not BHS_E_BHSA20150622035126.CSV .

File name : TestFile1_sorted.txt

BHS_E_BHAA20150622035126.CSV
BHS_E_BHSA20150622035126.CSV
BHS_E_CNBY20150622035126.CSV
BHS_E_PACS20150622035126.CSV
6
  • possible duplicate of Remove Top Line of Text File with Powershell Commented Jun 30, 2015 at 12:18
  • In case it is related can you show us what you mean by I need to use the output file inside the while loop statement for more than 5 times Commented Jun 30, 2015 at 13:10
  • I was using the variable $listing as the file name as below. Get-Content BHS_output.txt | Select-String -pattern $listing| sort | Foreach-Object {$_ -replace "$listing", ""} > $listing Where $listing is the variable from the first row of the file on first iteration. On the second iteration $listing = Get-Content .\file.txt | select-object -First 1 . $listing must have BHS_E_CNBY20150622035126.CSV .. Not BHS_E_BHSA20150622035126.CSV. Commented Jun 30, 2015 at 15:21
  • So i need to delete the first row from the file TestFile1_sorted.txt to proceed furthur.. I'm woking as a Sybase developer and this was my first project. Thanks in Advance Matt. Commented Jun 30, 2015 at 15:26
  • Can you edit your question with that information. Your question possible different then your title suggests. Commented Jun 30, 2015 at 15:36

3 Answers 3

1

The error you would be getting would be about saving to the same file you are reading from which is the product of the pipeline you created. Each line is processed one at a time so the second line is being written to file while the files is still open to keep reading.

(Get-Content $path) | Select-Object -Skip 1 | Set-Content $path

Putting the (Get-Content $path) in brackets will process the entire file into memory. Beware if the file is large. Then the rest of your code will work as normally.

Sign up to request clarification or add additional context in comments.

2 Comments

If both the path of Get-content and Set-content is same, Will it work? For me its throwing error as Unable to Open the document.
@JohnChristopher Yes this exact code will work assuming that the file is not already open and that you have the right permissions in the first place.
0

have you tried just "select" instead of "select-object"

Get-Content TestFile1_sorted.txt | Select -Skip 1

Someone answered a question about this before: Remove Top Line of Text File with Powershell

get-content $file |
    select -Skip 1 |
    set-content "$file-temp"
move "$file-temp" $file -Force

1 Comment

Select is the same as Select-Object one is an alias of the other.
0

you can do this:

$a = Get-Content C:\temp.txt
$a[1..-1] | Out-File c:\newfile.txt

if you want to overwrite the file replace the destination

what it does it creates new array and select it from the 2nd to the end

1 Comment

I want to use it inside the while loop Statement.So there should not be destination Folder in it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.