Solved

Remoing lines of text from a text file using powershell.

Posted on 2011-09-27
4
506 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
  • 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 68

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 68

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Synchronize a new Active Directory domain with an existing Office 365 tenant
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now