Solved

Remoing lines of text from a text file using powershell.

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

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 69

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Set OWA language and time zone in Exchange for individuals, all users or per database.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

785 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