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.