Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Remoing lines of text from a text file using powershell.

Posted on 2011-09-27
4
Medium Priority
?
517 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 71

Accepted Solution

by:
Qlemo earned 2000 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 71

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Starting up a Project

721 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