Solved

Remoing lines of text from a text file using powershell.

Posted on 2011-09-27
4
516 Views
Last Modified: 2012-05-12
Hi,

I am currently using this code to remove lines of text from a text file in a batch file and it works fine.

net use M: "\\Goldfinch\data"
M:
move DEB304INV.txt L:
move DEB304FIN.txt L:
move DEB304REM.txt L:
L:
more +29 "L:\DEB304REM.txt" > "L:\DEBTORS304REM.txt"
del L:\DEB304REM.txt
more +29 "L:\DEB304INV.txt" > "L:\DEBTORS304INV.txt"
del L:\DEB304INV.txt
ren DEB304FIN.txt DEBTORS304FIN.txt
net use M: /delete /y

I am moving all my batch files across to PowerShell, can you tell how to remove the lines out of the text file using PowerShell.

Thanks


Luke
0
Comment
Question by:luketr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 14

Expert Comment

by:athomsfere
ID: 36710378
Why change it at all? Just add it to your ps1 file... powershell can run all command line commands, unless there is another reason you want to change.
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 36711838
I took the liberty to "optimize" your script ...
set-location \\Goldfinch\data
"DEB304INV.txt","DEB304FIN.txt","DEB304REM.txt" | Move-Item L:\

Set-Location L:\
"REM","INV" | % { 
  gc "DEB304$_.txt" | select -Skip 29 | Out-File "DEBTORS304$_.txt"
  Remove-Item "DEB304$_.txt"
}
Rename-Item DEB304FIN.txt DEBTORS304FIN.txt

Open in new window

0
 

Author Closing Comment

by:luketr
ID: 36715132
That's brilliant thanks a lot for optimizing me script.

Thanks

Luke
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 36716074
There are some more possible changes to further optimize (remove redundancy), but they would not improve readability, so I refrained from suggesting them. Should you have many more files to treat all the same, it will be worth a thought to supply file name lists, maintained only once.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question