Collection of often needed finds and replaces in files

Find, replace, find in files, replace in files, regular expressions

Collection of often needed finds and replaces in files

Postby Mofi » Mon May 20, 2013 12:42 pm

This sticky topic is created for building a collection of often needed finds or replaces in

  • current file,
  • all open files,
  • or all files in a directory or even folder tree.
Every post with a general find or replace which might be useful for others is welcome on this topic.

Please give each post an individual subject explaining briefly what kind of task can be done with the posted find or replace.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Append line termination to end of file if last line has no line termination

Postby Mofi » Mon May 20, 2013 12:45 pm

Append DOS line termination to end of file if last line has no DOS line termination

With the Perl regular expression engine:

Find What: (.)$(?!\r\n)
Replace With: \1\r\n

With UltraEdit and Unix regular expression engines there is no expression to append a line termination to end of file.



Append UNIX line termination to end of file if last line has no UNIX line termination

With the Perl regular expression engine:

Find What: (.)$(?!\n)
Replace With: \1\n

This replace should be never executed on files with DOS line terminations (carriage return + linefeed). Use it only for files with UNIX line terminators (only linefeed) not converted temporarily to DOS on file load.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Trim trailing spaces and tabs from all lines

Postby Mofi » Mon May 20, 2013 12:48 pm

Trim trailing spaces and tabs from all lines

There is the command Format - Trim Trailing Spaces which can be used also in macros and scripts to remove all trailing spaces and tabs from all lines of active file.

But in case of the need to trim trailing spaces and tabs on all files in a directory or a directory tree, it is better to use a regular expression Replace in Files for this task.

With the Perl or Unix regular expression engine:

Find What: [ \t]+$
Replace With: empty

With the UltraEdit regular expression engine:

Find What: [ ^t]+$
Replace With: empty

Please note that trailing spaces/tabs at end of last line of a file are not removed when using UltraEdit or Unix regular expression engine if the last line has no line termination. The Perl regular expression works also on last line of a file without line termination.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Convert all files in a folder (tree) from DOS to UNIX or from UNIX to DOS

Postby Mofi » Mon May 20, 2013 12:53 pm

Convert all files in a folder (tree) from DOS to UNIX

There is the command File - Conversions - DOS to UNIX which can be used also in macros and scripts. It is strongly recommended to use this command on files opened already in UltraEdit or UEStudio. The command is also available from context menu of a file tab.

But in case of the need to convert all DOS line terminations to UNIX in all files in a directory or a directory tree, it is better to use a Replace in Files for this task.

With the Perl or Unix regular expression engine:

Find What: \r\n
Replace With: \n

With the UltraEdit regular expression engine or without using the regular expression option:

Find What: ^p
Replace With: ^n



Convert all files in a folder (tree) from UNIX to DOS

There is the command File - Conversions - UNIX/MAC to DOS which can be used also in macros and scripts. It is strongly recommended to use this command on files opened already in UltraEdit or UEStudio. The command is also available from context menu of a file tab.

But in case of the need to convert all UNIX line terminators to DOS in all files in a folder or a folder structure, it is better to use Replace in Files for this task.

Simply use following without using the regular expression option:

Find What: ^n
Replace With: ^p

But this can easily result in a carriage return + carriage return + linefeed sequence if some of the files contain already DOS line terminations. Therefore a second Replace in Files should be executed to make sure that such a line terminator sequence does not exist now in any file respectively correct them:

Find What: ^r^p
Replace With: ^p

This normal, non regular expression replace can be used also to fix CR CR LF sequences in files

  • created by applications opening a file in text mode and writing \r\n to the file instead of only \n as required in this case;
  • downloaded in ASCII mode from a webserver on which the files are stored already with DOS line terminations.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Find/Replace/Regular Expressions

cron