auto tighten columns (remove spaces)

Help with writing and running scripts

auto tighten columns (remove spaces)

Postby crcucb » Tue Feb 09, 2010 3:45 pm

Is there a way to auto tighten up columns, remove spaces in between columns but not enough to where the columns don't line up.

Example, here are 2 rows of data:
Code: Select all
123   12     12345
12     123   12345

I want to reformat it to:
Code: Select all
123 12   12345
12   123 12345

So the columns are still lined up but there is only 1 space after the widest value in a column.
crcucb
Newbie
 
Posts: 3
Joined: Mon Feb 01, 2010 10:05 am

Re: auto tighten columns (remove spaces)

Postby Mofi » Wed Feb 10, 2010 1:51 am

You can switch to column editing mode by pressing Alt+C or clicking on Column - Column Mode and then use Column - Delete Columns to delete those columns which contains only spaces.

Alternatively you could convert all spaces to tabs the get a character separated value file (CSV file) with the tab as separator by using a regular expression search searching for " +" (without the double quotes, i.e. space plus) and replace all occurrences with ^t (UE regexp) or \t (Unix/Perl regexp). Next use the command Column - Convert to Fixed Column. But the result would be slightly different because then all "field values" in the "columns" would be left aligned.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4056
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: auto tighten columns (remove spaces)

Postby crcucb » Wed Feb 10, 2010 10:18 am

Mofi wrote:Alternatively you could convert all spaces to tabs the get a character separated value file (CSV file) with the tab as separator by using a regular expression search searching for " +" (without the double quotes, i.e. space plus) and replace all occurrences with ^t (UE regexp) or \t (Unix/Perl regexp). Next use the command Column - Convert to Fixed Column. But the result would be slightly different because then all "field values" in the "columns" would be left aligned.

Thanks for the information.

This is going to be done with a macro/script and it's unknown the width of each column. When I select Column - Convert to Fixed Column it wants me to define each column size.

If I try to replace all tabs (or multiple spaces) with a single space (" ") then it removes a lot of the empty spaces but the columns don't remain aligned.
crcucb
Newbie
 
Posts: 3
Joined: Mon Feb 01, 2010 10:05 am

Re: auto tighten columns (remove spaces)

Postby Mofi » Thu Feb 11, 2010 2:14 pm

Here is a script to delete all multiple blank columns and additionally also all blank columns on left side. One blank column is kept between the values.

Code: Select all
function DeleteMultipleBlankColumns()
{
   UltraEdit.ueReOn();
   UltraEdit.insertMode();
   UltraEdit.columnModeOff();
   // Set the cursor to last non whitespace character at end of file and
   // get the number of the line containing the last non whitespace char.
   UltraEdit.activeDocument.bottom();
   UltraEdit.activeDocument.findReplace.mode=0;
   UltraEdit.activeDocument.findReplace.matchCase=false;
   UltraEdit.activeDocument.findReplace.matchWord=false;
   UltraEdit.activeDocument.findReplace.regExp=true;
   UltraEdit.activeDocument.findReplace.searchAscii=false;
   UltraEdit.activeDocument.findReplace.searchDown=false;
   UltraEdit.activeDocument.findReplace.searchInColumn=false;
   UltraEdit.activeDocument.findReplace.find("[~ ^t^r^n^b]");
   if (UltraEdit.activeDocument.isNotFound()) return;
   UltraEdit.activeDocument.findReplace.searchDown=true;
   UltraEdit.activeDocument.key("END");
   // Inserting the following text should make the last line longer than all other lines.
   UltraEdit.activeDocument.write("                                                            <InSeRtEd>");
   var nNumberOfLastLine = UltraEdit.activeDocument.currentLineNum;

   // Move cursor to top of file and delete all trailing spaces in the file.
   UltraEdit.activeDocument.top();
   UltraEdit.activeDocument.trimTrailingSpaces();
   // For ignoring blank lines at top of the file search for first non
   // whitespace character at top of the file and remember the number
   // of the line where this non whitespace character was found.
   UltraEdit.activeDocument.findReplace.find("[~ ^t^r^n^b]");
   UltraEdit.activeDocument.key("HOME");
   var nNumberOfFirstLine = UltraEdit.activeDocument.currentLineNum;

   // Enable column editing mode and set search option
   // to run all further searches in selected text only.
   UltraEdit.columnModeOn();
   UltraEdit.activeDocument.findReplace.mode=1;

   // Blank columns on most left side are always completely deleted.
   var bFirstBlankColumnFound = true;
   // For every character on the first non blank line.
   var nColumn = 0;
   while(!UltraEdit.activeDocument.isChar("\r") && !UltraEdit.activeDocument.isChar("\n")) {

      UltraEdit.activeDocument.gotoLine(nNumberOfFirstLine,++nColumn);
      // Is the character NOT a space, nothing to do on this column.
      if (!UltraEdit.activeDocument.isChar(" ")) {
         bFirstBlankColumnFound = false;
         continue;
      }
      // Select to next column of last line resulting in selecting this column.
      UltraEdit.activeDocument.gotoLineSelect(nNumberOfLastLine,nColumn+1);
      // Run a regular expression search to check if there is any non whitespace
      // character in the selected column which results in keeping the column as is.
      UltraEdit.activeDocument.findReplace.find("[~ ^t^r^n^b]");
      var bNonSpace = UltraEdit.activeDocument.isFound();
      UltraEdit.activeDocument.endSelect();
      if (bNonSpace) continue;
      // The column contains only spaces. Delete this column, except
      // it is the first blank column which must be always kept.
      if (!bFirstBlankColumnFound) bFirstBlankColumnFound = true;
      else {
         UltraEdit.activeDocument.gotoLine(nNumberOfFirstLine,nColumn--);
         UltraEdit.activeDocument.columnDelete(1);
      }
   }
   // Turn off column editing mode, delete the inserted text at bottom of the
   // file and go back to top of the file before exiting this script function.
   UltraEdit.columnModeOff();
   UltraEdit.activeDocument.findReplace.matchCase=true;
   UltraEdit.activeDocument.findReplace.regExp=true;
   UltraEdit.activeDocument.findReplace.mode=0;
   UltraEdit.activeDocument.findReplace.find(" +<InSeRtEd>");
   UltraEdit.activeDocument.deleteText();
   UltraEdit.activeDocument.top();
}

if (UltraEdit.document.length > 0) {
   DeleteMultipleBlankColumns();
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4056
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Scripts