Solved

Remoing lines of text from a text file using powershell.

Posted on 2011-09-27
4
510 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article will help you understand what HashTables are and how to use them in PowerShell.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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 …

914 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

16 Experts available now in Live!

Get 1:1 Help Now