The Multifunctioning DBA

Apr 19 2010   8:12PM GMT

Quick Powershell script to clean up text file

Colin Smith Colin Smith Profile: Colin Smith

I had a script that was a list of user tables in a database. This was gathered from a sybase instance using the following simple sql query.

select name from sysobjects

where type = ‘U’

Simple enough but my output had blank lines between entries and this was no good. I needed to strip the spaces and new lines out as well as add a database name to the begining of each table name. I needed that for the script that is going to use this file as an input. Here is what I did in Powershell to make it all work.

$tables = $NULL
$tables = Get-Content “c:\tables.txt”
foreach($table in $tables)
 {
  $table = $table.trim()
  $table = “DBNAME.$table”
  $table >> “c:\newtables.txt”
 }

 

So I had a input file name tables.txt and that had my list of tables. Formatting was wrong and I needed the DBNAME.tablename to be in the new file.

I simply read the file in and go thru each line. I use the trim() function to get rid of all spaces and then I add “DBNAME.” to the front of the table name. I then output that to a file called newtables.txt and now I have the input file, properly formatted, for the script that needs it.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: